-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.cpp
More file actions
53 lines (53 loc) · 1.33 KB
/
Anagrams.cpp
File metadata and controls
53 lines (53 loc) · 1.33 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
47
48
49
50
51
52
53
class Solution
{
public:
/*
Anagrams
problem:Given an array of strings, return all groups of strings that are anagrams.
analysis:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序。 因此,将几个单词按照字母顺序排序后,若它们相等,则它们属于同一组 anagrams 。
*/
vector<string> anagrams(vector<string> &strs)
{
unordered_map<string, vector<string>> group;
for (const auto &s : strs)
{
string key = s;
sort(key.begin(), key.end());
group[key].push_back(s);
}
vector<string> result;
for (auto it = group.cbegin(); it != group.cend(); it++)
{
if (it->second.size() > 1)
result.insert(result.end(), it->second.begin(), it->second.end());
}
return result;
}
/*Group Anagrams*/
vector<vector<string>> groupAnagrams(vector<string>& strs)
{
unordered_map<string, vector<string>> group;
for (const auto &s : strs)
{
string key = s;
sort(key.begin(), key.end());
group[key].push_back(s);
}
vector<vector<string>> result;
for (auto it = group.cbegin(); it != group.cend(); it++)
{
vector<string> temp = it->second;
sort(temp.begin(), temp.end());
result.push_back(temp);
}
return result;
}
/*valid anagram , 判断两个字符串是不是anagram*/
bool isAnagram(string s, string t)
{
sort(s.begin(), s.end());
sort(t.begin(), t.end());
if (s == t) return true;
else return false;
}
}