Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
anagram
//Title of this code #include <iostream> #include <vector> #include <map> #include <string> #include <list> using namespace std; map<char, int> getCharMap(const string& s) { map<char, int> m; for (int i = 0; i < s.length(); ++i) { if (m.find(s[i]) == m.end()) m[s[i]] = 1; else ++m[s[i]]; } return m; } bool areCharMapEqual(map<char, int>& m1, map<char, int>& m2) { if (m1.size() != m2.size()) return false; auto it1 = m1.begin(); auto it2 = m2.begin(); // the sizes are the same so we don't need further checking while (it1 != m1.end()) { if (it1->first != it2->first || it1->second != it2->second) return false; ++it1; ++it2; } return true; } struct StringAndCharMap { string str; map<char, int> strMap; StringAndCharMap(string s, map<char, int> m): str(s), strMap(m) {} }; void findAnagrams(vector<string>& words) { vector<StringAndCharMap> wordsCharMaps; for (int i = 0; i < words.size(); ++i) wordsCharMaps.push_back(StringAndCharMap(words[i], getCharMap(words[i]))); int i = 0; while (!wordsCharMaps.empty()) { cout << wordsCharMaps[i].str << " " for (int j = i + 1; j < wordsCharMaps.size(); ++j) { if (areCharMapEqual(wordsCharMaps[j].strMap, wordsCharMaps[i].strMap)) { cout << wordsCharMaps[j].str << " "; wordsCharMaps.erase(); } } cout << endl; } for (int i = 0; i < wordsCharMaps.size(); ++i) { cout << i << ". " << wordsCharMaps[i].str << " "; for (int j = 0; j < wordsCharMaps.size(); ++j) { if (i != j && areCharMapEqual(wordsCharMaps[i].strMap, wordsCharMaps[j].strMap)) cout << j << ". " << wordsCharMaps[j].str << " "; } cout << endl; } /* map<string, list<string> > anagrams; anagrams[words[0]] = list<string>(); for (int i = 1; i < wordsCharMaps.size(); ++i) { for (auto it = anagrams.begin(); it != anagrams.end(); ++it) { wordsCharMaps[i].strMap } } */ /* map<string, list<string> > anagrams; anagrams[words[0]] = list<string>(); for (int i = 0; i < words.size(); ++i) { bool found = false; for (int j = i + 1; j < words.size(); ++j) if (areCharMapEqual(wordsCharMaps[i], wordsCharMaps[j])) { } } */ /* vector<vector<string> > anagrams; anagrams.push_back(vector<string>({words[0]})); for (int i = 1; i < words.size(); ++i) { for (int j = 0; j < anagrams.size(); ++j) if (anagrams[j][0]) } */ /* for (int i = 0; i < wordsCharMaps.size(); ++i) for (int j = i + 1; j < wordsCharMaps.size(); ++j) areCharMapEqual(wordsCharMaps[i], wordsCharMaps[i]) */ } int main() { vector<string> words = {"amazon", "zonama", "", "ammazon", "zonnama"}; findAnagrams(words); /* map<char, int> m1 = getCharMap("ammz"); map<char, int> m2 = getCharMap("zamm"); cout << boolalpha << areCharMapEqual(m1, m2); */ }
run
|
edit
|
history
|
help
0
RegexSearch
TraiectorieIdeala
LIS
Problem D
CPP - Pointers - Ex.1
multiply_without_asterisk
PriorQ
Example Iterator Increment
Rzutka gra 2
DFS