-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.cpp
33 lines (28 loc) · 922 Bytes
/
anagram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> result;
map<string,vector<string> > mark;
while(!strs.empty()){
string str = strs.back();
strs.pop_back();
string sortstr = str;
sort(sortstr.begin(),sortstr.end());
mark[sortstr].push_back(str);
}
map<string, vector<string> >::iterator it = mark.begin();
while(it!=mark.end()){
vector<string> tmp = it->second;
if(tmp.size() > 1){
while(!tmp.empty()){
result.push_back(tmp.back());
tmp.pop_back();
}
}
it++;
}
return result;
}
};