Problem:
Given an array of strings, return all groups of strings that is anagrams.
Note:all inputs'll is in lower-case.
Hide TagsHash Table StringTest instructions: Given the extra two sets of strings, find all of the strings that meet the following criteria: (1) The character type of the string is the same as the corresponding number (2) the position of each character is not differentiated
Thinking:
(1) It is very straightforward to think of using hash table to solve this kind of problem
(2) can also borrow STL map, more convenient, primary key key to store a sorted String,value store pointer to the string
Map<string, vector<const string *> > Mapanagram;
Code
Class solution{ public:vector<string> anagrams (vector<string> &strs) { vector<string > ret; Map<string, vector<const string *> > Mapanagram; for (Vector<string>::const_iterator it = Strs.begin (); It! = Strs.end (); ++it) { string key (*it); Sort (Key.begin (), Key.end ()); Mapanagram[key].push_back (&*it); } For (map<string, vector<const string *> >::const_iterator it = Mapanagram.begin (); It! = Mapanagram.end (); ++it) { if (it->second.size () > 1) {for (Vector<const string *>::const_iterator Itstr = It->second.begin (); Itstr! = It->second.end (); ++ITSTR) { ret.push_back (**ITSTR);}} } return ret;}};
Leetcode | | 49, Anagrams