forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.cc
More file actions
23 lines (23 loc) · 768 Bytes
/
Anagrams.cc
File metadata and controls
23 lines (23 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
map<string, vector<string> > mp;
vector<string> ans;
string tmp;
for (int i = 0; i < strs.size(); i++) {
tmp = strs[i];
sort(tmp.begin(), tmp.end());
mp[tmp].push_back(strs[i]);
}
for (map<string, vector<string> >::iterator p = mp.begin(); p != mp.end(); p++) {
if ((p->second).size() > 1) {
for (int i = 0; i < (p->second).size(); i++) {
ans.push_back((p->second)[i]);
}
}
}
return ans;
}
};