乱序字符串
描述
笔记
数据
评测
给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。
注意事项
所有的字符串都只包含小写字母
您在真实的面试中是否遇到过这个题? Yes
样例
对于字符串数组 [“lint”,”intl”,”inlt”,”code”]
返回 [“lint”,”inlt”,”intl”]
class Solution {
public:
/**
* @param strs: A list of strings
* @return: A list of strings
*/
vector<string> anagrams(vector<string> &strs) {
// write your code here
map<string,int> m;vector<string> v;
for(auto s:strs){
sort(s.begin(),s.end());
m[s]++;
}
for(auto &s:strs){
string s1=s;
sort(s1.begin(),s1.end());
if(m[s1]>1)
v.push_back(s);
}
return v;
}
};