-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathanagrams.cpp
More file actions
21 lines (21 loc) · 767 Bytes
/
anagrams.cpp
File metadata and controls
21 lines (21 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution { //a very violent method
public:
vector<string> anagrams(vector<string> &strs) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
map<string, vector<string> > m;
vector<string> ans;
for(int i = 0; i < strs.size(); ++i) {
string tmp = strs[i];
sort(tmp.begin(), tmp.end());
m[tmp].push_back(strs[i]);
}
for(map<string, vector<string> >::iterator p = m.begin(); p != m.end(); ++p) {
if(p->second.size() > 1) {
for(int i = 0; i < p->second.size(); ++i)
ans.push_back(p->second[i]);
}
}
return ans;
}
};