/
Anagrams.cpp
31 lines (30 loc) · 918 Bytes
/
Anagrams.cpp
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
27
28
29
30
31
/*
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
*/
class Solution {
public:
vector<string> anagrams(vector<string>& strs) {
vector<string> result;
if (strs.size() <= 1) {
return result;
}
map<string, int> myMap;
for (int i = 0; i < strs.size(); i++) {
string current = strs[i];
sort(current.begin(), current.end());
if (myMap.find(current) != myMap.end()) {
if (myMap[current] != -1) {
result.push_back(strs[myMap[current]]);
result.push_back(strs[i]);
myMap[current] = -1;
} else {
result.push_back(strs[i]);
}
} else {
myMap[current] = i;
}
}
return result;
}
};