-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.h
More file actions
26 lines (25 loc) · 791 Bytes
/
Anagrams.h
File metadata and controls
26 lines (25 loc) · 791 Bytes
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
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
map<string, vector<string>> mmap;
for(int i = 0; i < strs.size(); ++i){
string str = strs[i];
sort(str.begin(), str.end());
if(mmap.find(str) != mmap.end()){
(mmap[str]).push_back(strs[i]);
} else {
vector<string> tmpv;
tmpv.push_back(strs[i]);
mmap[str] = tmpv;
}
}
vector<string> ret;
for(auto it = mmap.begin(); it != mmap.end(); ++it) {
if(it->second.size() > 1){
for(int i = 0; i < it->second.size(); ++i)
ret.push_back(it->second[i]);
}
}
return ret;
}
};