-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.cpp
More file actions
24 lines (24 loc) · 754 Bytes
/
Copy pathAnagrams.cpp
File metadata and controls
24 lines (24 loc) · 754 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
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> r;
map<string, vector<string> > anagrams_map;
for (int i = 0; i < strs.size(); i++) {
string sorted_str = strs[i];
sort(sorted_str.begin(), sorted_str.end());
anagrams_map[sorted_str].push_back(strs[i]);
}
for (auto map_iter = anagrams_map.begin(); map_iter != anagrams_map.end();
map_iter++) {
vector<string> &anagrams_vec = map_iter->second;
if (anagrams_vec.size() == 1)
continue;
for (int i = 0; i < anagrams_vec.size(); i++) {
r.push_back(anagrams_vec[i]);
}
}
return r;
}
};