题目要求:Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
分析:
参考网址:http://www.cnblogs.com/easonliu/p/3643595.html
代码如下:
class Solution { public: vector<string> anagrams(vector<string> &strs) { string s; map<string, int> anagram; vector<string> res; for (int i = 0; i < strs.size(); ++i) { s = strs[i]; sort(s.begin(), s.end()); if (anagram.find(s) == anagram.end()) { anagram[s] = i; } else { if (anagram[s] >= 0) { res.push_back(strs[anagram[s]]); anagram[s] = -1; } res.push_back(strs[i]); } } return res; } };