-
Notifications
You must be signed in to change notification settings - Fork 5
/
Anagrams.cpp
54 lines (40 loc) · 1.18 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
input output expected
[""] [] []
["a"] [] []
["","b"] [] []
["c","c"] ["c","c"] ["c","c"]
["ant","ant"] ["ant","ant"] ["ant","ant"]
["and","dan"] ["and","dan"] ["and","dan"]
["ape","and","cat"] [] []
["ape","pea","tax"] ["ape","pea"] ["ape","pea"]
["ate","eat","tea"] ["ate","eat","tea"] ["ate","eat","tea"]
["tea","and","ate","eat","den"] ["tea","ate","eat"] ["tea","ate","eat"]
["tea","and","ate","eat","dan"] ["tea","and","ate","eat","dan"] ["tea","and","ate","eat","dan"]
["tea","and","ace","ad","eat","dans"] ["tea","eat"] ["tea","eat"]
*/
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
map<string, int> m;
vector<string> result;
int n = strs.size();
for (int i = 0; i < n; i++) {
string s2 = strs[i];
sort(s2.begin(), s2.end());
m[s2]++;
}
for (int i = 0; i < n; i++) {
string s2 = strs[i];
sort(s2.begin(), s2.end());
if (m[s2] > 1)
result.push_back(strs[i]);
}
return result;
}
};