Anagrams
Q:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
A: 重点是理解变位词的概念及其给所有词打tag:字典序
vector<string> anagrams(vector<string> &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<string> res; if(strs.empty()) return res; map<string,vector<string>> dict; for(int i=0;i<strs.size();i++) { string key = strs[i]; sort(key.begin(),key.end()); dict[key].push_back(strs[i]); } map<string,vector<string>>::iterator it; for(it = dict.begin();it!=dict.end();it++) { if(it->second.size()>1) { vector<string>::iterator vt; for(vt=it->second.begin();vt!=it->second.end();vt++) res.push_back(*vt); } } return res; }