Problem Description:
Given an array of strings, return all groups of strings that is anagrams.
Note:all inputs'll is in lower-case.
Basic ideas:
Sorts each string, and then counts more than 1 occurrences of the sorted string. Add the original string of these strings more than 1 times to the result set
Code:
Vector<string> anagrams (vector<string> &strs) { //c++ vector<string> result; if (strs.size () = = 0) return result; Vector<string> Vec (Strs.begin (), Strs.end ()); for (int i = 0; i < vec.size (); i++) sort (Vec[i].begin (), Vec[i].end ()); Map<string,int> TMap; for (int i = 0; i < vec.size (); i++) tmap[vec[i]] + = 1; Set<string> Tset; for (Map<string,int>::iterator iter = Tmap.begin (); ITER! = Tmap.end (); iter++) if (Iter->second > 1) C13/>tset.insert (Iter->first); for (int i = 0; i < vec.size (); i++) { if (Tset.count (vec[i]) = = 1) result.push_back (Strs[i]); } return result; }
[Leetcode]