-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.cpp
More file actions
24 lines (23 loc) · 788 Bytes
/
Anagrams.cpp
File metadata and controls
24 lines (23 loc) · 788 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
//O(N*log(N))
//O(N)
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
map<string, vector<string> > buffer;
for (int i = 0; i < strs.size(); i++) {
string copy(strs[i]);
sort(copy.begin(), copy.end());
buffer[copy].push_back(strs[i]);
}
vector<string> res;
map<string, vector<string> >::iterator it = buffer.begin();
while (it != buffer.end()) {
vector<string> sub = it->second;
if (sub.size() > 1)
for (int i = 0; i < sub.size(); i++)
res.push_back(sub[i]);
it++;
}
return res;
}
};