-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagrams.cpp
More file actions
32 lines (32 loc) · 832 Bytes
/
anagrams.cpp
File metadata and controls
32 lines (32 loc) · 832 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
30
31
32
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string>ret;
if (strs.empty())
{
return ret;
}
map<string, int>ma;
vector<bool>pu(strs.size(), false);
for (unsigned int i = 0; i < strs.size(); i++)
{
auto str = strs[i];
sort(str.begin(), str.end());
if (ma.count(str) == 0)
{
ma[str] = i;
}
else
{
if (pu[ma[str]] == false)
{
ret.push_back(strs[ma[str]]);
pu[ma[str]] = true;
}
ret.push_back(strs[i]);
pu[i] = true;
}
}
return ret;
}
};