Topic:
Given an array of strings, return all groups of strings that is anagrams.
Note:all inputs'll is in lower-case.
Code:
classSolution { Public: Vector<string> anagrams (vector<string>&STRs) {Vector<string>ret; Map<string,vector<int> >Str_indexs; //Trans STRs to key (sorted string) and value (Indexs of STRs, that has same key) pairs for(size_t i =0; I < strs.size (); ++i) {stringKey =Strs[i]; Std::sort (Key.begin (), Key.end ()); Str_indexs[key].push_back (i); } //Add the keys which occurs more than once for(map<string,vector<int> >::iterator it = str_indexs.begin (); It!= Str_indexs.end (); ++it) { if(It->second.size () >1 ) { for(size_t k =0; K < It->second.size (); ++k) {Ret.push_back (Strs[it-Second[k]]); } } } returnret; }};
Tips
The use of Std::map is not familiar, and then the problem is familiar.
This problem is very classic, direct search of AC code. Learn a bit, and read a few blogs recorded below.
Http://bangbingsyb.blogspot.sg/2014/11/leetcode-anagrams.html
Http://www.cnblogs.com/AnnieKim/archive/2013/04/25/3041982.html
"Anagrams" CPP