Anagrams

来源:互联网 发布:获取数据失败怎么办 编辑:程序博客网 时间:2024/06/04 19:15

 

Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

 

 

anagrams:两个字符串,字母相同,出现次数相同的两个字符串互为anagrams

 

 

class Solution {public:    vector<string> anagrams(vector<string> &strs) {map<string, int> count;for(int i =0; i < strs.size(); ++i){string tmp = strs[i];sort(tmp.begin(), tmp.end());if(count.find(tmp) == count.end())count[tmp] = 1;else count[tmp]++;}vector<string> rst;for(int i = 0; i < strs.size(); ++i){string tmp = strs[i];sort(tmp.begin(), tmp.end());if(count[tmp] > 1)rst.push_back(strs[i]);}return rst;    }};


 

0 0