-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagrams.cpp
More file actions
46 lines (37 loc) · 1.1 KB
/
Copy pathanagrams.cpp
File metadata and controls
46 lines (37 loc) · 1.1 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
/**
* @param strs: A list of strings
* @return: A list of strings
*/
vector<string> anagrams(vector<string> &strs) {
// write your code here
unordered_map<string, pair<string, bool>> map;
vector<string> result;
for (auto &i : strs) {
string key = keyGen(i);
if (map.find(key) != map.end()) {
if (map[key].second == false) {
result.push_back(map[key].first);
map[key].second = true;
}
result.push_back(i);
} else {
map.insert(make_pair(key, make_pair(i, false)));
}
}
return result;
}
string keyGen(string &str) {
vector<int> count(26, 0);
for (auto &i: str) {
count[i -'a']++;
}
string result("");
for (int i = 0; i < 26; i++) {
result += to_string(count[i]);
result.append("|");
}
return result;
}
};