From b2248fbd7224d61c3a2ba60faa2a6f9109b49b12 Mon Sep 17 00:00:00 2001 From: Muhammad Arham Jan <146324130+ArhamJan19@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:12:56 +0500 Subject: [PATCH] Arhams Updated version of anagrams.cpp Updated the existing file anagrams.cpp --- c++/anagrams.cpp | 84 +++++++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 29 deletions(-) diff --git a/c++/anagrams.cpp b/c++/anagrams.cpp index b9d855a..36aa8fc 100644 --- a/c++/anagrams.cpp +++ b/c++/anagrams.cpp @@ -1,29 +1,55 @@ -class Solution { -public: - vector anagrams(vector &strs) { - /* https://oj.leetcode.com/problems/anagrams/ - ["abc", "ef", "cba", "xy", "yx"] -> ["abc", "cba", "xy", "yx"] - the order doesn't matter - */ - - vector result; - unordered_map dict; - - for (int i = 0; i < strs.size(); i++) { - string s = strs[i]; - sort(s.begin(), s.end()); - if (dict.find(s) == dict.end()) { - dict[s] = strs[i]; - } - else { - if (dict[s] != "#") { - result.push_back(dict[s]); - dict[s] = "#"; - } - result.push_back(strs[i]); - } - } - - return result; - } -}; \ No newline at end of file +#include +#include +using namespace std; +int main() +{ + string first = ""; + string second = ""; + cout << "\nEnter the First Word: "; + getline(cin, first); + cout << "\nEnter the Second Word: "; + getline(cin, second); + int letters_counter=0; + if (first.length() == second.length()) + { + bool found = false; + + for (int i = 0; i < first.length(); i++) + { + for (int j = 0; j < second.length(); j++) + { + if (first[i] == second[j]) + { + found = true; + break; + } + + } + + if (found) + { + letters_counter += 1; + } + } + if (letters_counter == first.length()) + { + cout << "\nANAGRAMS"; + } + else + { + cout << "\nNOT ANAGRAMS"; + } + + } + else + { + cout << "\nNOT ANAGRAMS"; + cout << endl << endl; + system("pause"); + return 0; + } + + cout << endl << endl; + system("pause"); + return 0; +}