LeetCode/solutions/49. Group Anagrams.md

85 lines
3.6 KiB
Markdown
Raw Normal View History

2019-01-14 15:59:50 +00:00
# [49. Group Anagrams](https://leetcode.com/problems/group-anagrams/)
# 思路
2020-06-20 02:43:47 +00:00
## 思路一
2019-01-14 15:59:50 +00:00
题目要求找出字符串数组里的anagram(同字母异序词),所有的字母都是小写字母。
如何判断两个字符串是否是anagram呢有两种方法:
1. 先排序再判断排序后的字符串是否相等;
2. 遍历一遍然后用一个数组记录26个字母每个字母出现的次数再比较这个次数类似桶排序
如果方法1使用桶排序的话其实两种方法就是差不多的了。
2019-01-14 16:03:20 +00:00
解决了如何判断的问题,算法思路就清晰了:
对strs中的每个字符串的副本str对其进行排序可以使用桶排序也可使用现成的sort然后用一个hash表mp来记录这个str是否在以前出现过。
若`mp[str] == 0`则说明没有出现过否则则出现过mp[str]的值代表最终返回结果数组即代码中的res中对应元素的下标加一。
2019-01-14 15:59:50 +00:00
若strs中有n个字符串每个字符串长度平均为m则桶排序时间复杂度为O(m)如果使用的是unordered_map对应hash则查找复杂度O(1)所以总体时间复杂度O(mn)。
2020-06-20 02:43:47 +00:00
注意: unordered_map对应hash比map对应红黑树快一些所以使用map的时候如果追求时间复杂度则一律使用unordered_map。
## 思路二
讨论区还有一种比较tricky的方法思路一我们先进行排序的目的是方便后续判断【26个字母出现次数】是否完全一样如果一样就是anagram。我们也可以将26个字母用26个不同的素数代替然后将字符串中所有字母对应的素数乘起来如果两个字符串最后得到的乘积相等那么是anagram。
注意这个乘积可能很大亲测long long还会溢出unsigned long long才不会溢出。所以说此种方法仅供开阔思路。
时间复杂度同思路一
2019-01-14 15:59:50 +00:00
# C++
2020-06-20 02:43:47 +00:00
## 思路一
2019-01-14 15:59:50 +00:00
``` C++
class Solution {
private:
string count_sort(const string &str){ // 桶排序返回str排序后的副本
int count[26] = {0};
for(char c: str) count[c - 'a']++;
string res;
for(int i = 0; i < 26; i++)
res.append(count[i], i + 'a'); // 在res后面加上count[i]个字符 i + 'a'
return res;
}
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>>res;
unordered_map<string, int>mp;
int n = strs.size();
for(int i = 0; i < n; i++){
string str = count_sort(strs[i]); // 使用桶排序理论上会快一些
// string str = strs[i];
// sort(str.begin(), str.end());
int tmp = mp[str];
if(tmp == 0) { // 没出现过
res.push_back(vector<string>(1, strs[i]));
mp[str] = res.size(); // map[str]的值代表res对应元素的下标加一
}
else
res[tmp - 1].push_back(strs[i]);
}
return res;
}
};
```
2020-06-20 02:43:47 +00:00
## 思路二
``` C++
class Solution {
public:
vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>>res;
unordered_map<unsigned long long, int>mp;
for(string s: strs){
unsigned long long hash = 1;
for(char c: s)
hash *= primes[c - 'a'];
if(!mp.count(hash)){
res.push_back({s});
mp[hash] = res.size();
}
else res[mp[hash] - 1].push_back(s);
}
return res;
}
};
```