Skip to content

Commit 63e1465

Browse files
committed
49 Anagrams
1 parent 412926f commit 63e1465

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
+ [43 Multiply Strings(大数乘法)](algorithms/MultiplyStrings)
3333
+ [46 Permutations(全排列)](algorithms/Permutations)
3434
+ [47 Permutations II(全排列)](algorithms/Permutations2)
35+
+ [49 Anagrams(字符串,回文构词)](algorithms/Anagrams)
3536
+ [50 Pow(x, n)(二分,浮点数比较,INT\_MIN绝对值)](algorithms/Pow)
3637
+ [58 Length of Last Word](algorithms/LengthofLastWord)
3738
+ [60 Permutation Sequence(康托展开)](algorithms/PermutationSequence)

algorithms/Anagrams/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Anagrams
2+
3+
Given an array of strings, return all groups of strings that are anagrams.
4+
5+
Note: All inputs will be in lower-case.
6+
7+
## Solution
8+
9+
两个单词的字符相同,只是排列顺序不同,称为回文构词法(anagrams), 比如tea eta ate aet都是anagrams。
10+
11+
题目要求找出所有的anagrams组,比如`["dog","cat","god","tac", "hello", "world"]`, 应该返回`["dog","cat","god","tac"]`,
12+
因为dog 和 god是一组, cat和tac是一组,即
13+
14+
```
15+
[
16+
["dog", "god"],
17+
["cat", "tac"]
18+
]
19+
```
20+
21+
判断两个字符串是不是anagrams,最简单的方法是对字符排序然后判断是否相等。比如`tac``cat`按字符排序后分别为`act``act`,显然是相等的,因此互为anagrams.
22+
23+
给定一个字符串数组,如何找出所有的anagrams组。当一个字符串处理时,我们需要判断之前是否出现过互为anagrams的字符串。
24+
如果没有出现过,我们保存到一个map中,key保存字符串的排序后的字符串,value保存第一次出现的索引。若已经出现过,并且当前是
25+
第二次出现,则前一次出现的和当前的字符串互为anagrams
26+
27+
```cpp
28+
vector<string> anagrams(const vector<string>& strs) {
29+
unordered_map<string, int> m;
30+
vector<string> result;
31+
for (int i = 0; i < strs.size(); ++i) {
32+
string s = strs[i];
33+
sort(begin(s), end(s));
34+
if (m.find(s) == m.end()) {
35+
m[s] = i;
36+
} else {
37+
if (m[s] >= 0) {
38+
result.push_back(strs[m[s]]);
39+
m[s] = -1;
40+
}
41+
result.push_back(strs[i]);
42+
}
43+
}
44+
return result;
45+
}
46+
```

algorithms/Anagrams/solve.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <string>
2+
#include <algorithm>
3+
#include <iostream>
4+
#include <unordered_map>
5+
using namespace std;
6+
class Solution {
7+
public:
8+
vector<string> anagrams(const vector<string>& strs) {
9+
unordered_map<string, int> m;
10+
vector<string> result;
11+
for (int i = 0; i < strs.size(); ++i) {
12+
string s = strs[i];
13+
sort(begin(s), end(s));
14+
if (m.find(s) == m.end()) {
15+
m[s] = i;
16+
} else {
17+
if (m[s] >= 0) {
18+
result.push_back(strs[m[s]]);
19+
m[s] = -1;
20+
}
21+
result.push_back(strs[i]);
22+
}
23+
}
24+
return result;
25+
}
26+
};
27+
int main(int argc, char **argv)
28+
{
29+
Solution solution;
30+
vector<string> strs = {"tea", "and", "ate", "eat", "den"};
31+
auto result = solution.anagrams(strs);
32+
for_each(begin(result), end(result), [](string s) {cout << s << " ";});
33+
cout << endl;
34+
return 0;
35+
}

0 commit comments

Comments
 (0)