Topic:
Given an array of strings, combine the letters with different words. Alphabetic synonyms refer to the same letters but arrange different strings. Example: input: ["Eat", "tea", "tan", "ate", "Nat", "bat"], output: [ "ate", "eat", "tea"], ["Nat", "Tan"], ["bat"]] Note: All inputs are lowercase letters. The order in which the answer output is not considered.
Solution:
Class Solution {public:vector<vector<string>> Groupanagrams (vector<string>& strs) {MAP&L T;string,set<int>> Str_map; vector<vector<string>> Res; for (int i = 0;i < Strs.size (); ++i) {string tmp = Strs[i]; Sort (Tmp.begin (), Tmp.end ()); if (Str_map.find (TMP)!=str_map.end ()) {Str_map[tmp].insert (i); }else{set<int> t; T.insert (i); Str_map.insert (Make_pair (tmp,t)); }} Map<string,set<int>>::iterator it = Str_map.begin (); for (; It!=str_map.end (); ++it) {vector<string> Str_vec; Set<int>::iterator set_it = (it->second). Begin (); for (; set_it!= (It->second). end (); ++set_it) {str_vec.push_back (strs[*set_it]); } res.push_back (Str_vec); } return res; }};
"Intermediate algorithm" 49. Alphabetical Grouping of synonyms