-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsolve.cpp
35 lines (35 loc) · 806 Bytes
/
solve.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
32
33
34
35
#include <string>
#include <algorithm>
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<string> anagrams(const vector<string>& strs) {
unordered_map<string, int> m;
vector<string> result;
for (int i = 0; i < strs.size(); ++i) {
string s = strs[i];
sort(begin(s), end(s));
if (m.find(s) == m.end()) {
m[s] = i;
} else {
if (m[s] >= 0) {
result.push_back(strs[m[s]]);
m[s] = -1;
}
result.push_back(strs[i]);
}
}
return result;
}
};
int main(int argc, char **argv)
{
Solution solution;
vector<string> strs = {"tea", "and", "ate", "eat", "den"};
auto result = solution.anagrams(strs);
for_each(begin(result), end(result), [](string s) {cout << s << " ";});
cout << endl;
return 0;
}