forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
29 lines (29 loc) · 703 Bytes
/
solution.cpp
File metadata and controls
29 lines (29 loc) · 703 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
25
26
27
28
29
class Solution
{
public:
vector<string> anagrams(vector<string> &strs)
{
string s;
map<string, int> anagram;
vector<string> result;
for (int i = 0; i < strs.size(); ++i)
{
s = strs[i];
sort(s.begin(), s.end());
if (anagram.find(s) == anagram.end())
{
anagram[s] = i;
}
else
{
if (anagram[s] >= 0)
{
result.push_back(strs[anagram[s]]);
anagram[s] = -1;
}
result.push_back(strs[i]);
}
}
return result;
}
};