Given an array of strings, group anagrams together.

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]

Note:

  • All inputs will be in lowercase.
  • The order of your output does not matter.

这道题让我们群组给定字符串集中所有的错位词,所谓的错位词就是两个字符串中字母出现的次数都一样,只是位置不同,比如 abc,bac, cba 等它们就互为错位词,那么如何判断两者是否是错位词呢,可以发现如果把错位词的字符顺序重新排列,那么会得到相同的结果,所以重新排序是判断是否互为错位词的方法,由于错位词重新排序后都会得到相同的字符串,以此作为 key,将所有错位词都保存到字符串数组中,建立 key 和当前的不同的错位词集合个数之间的映射,这里之所以没有建立 key 和其隶属的错位词集合之间的映射,是用了一个小 trick,从而避免了最后再将 HashMap 中的集合拷贝到结果 res 中。当检测到当前的单词不在 HashMap 中,此时知道这个单词将属于一个新的错位词集合,所以将其映射为当前的错位词集合的个数,然后在 res 中新增一个空集合,这样就可以通过其映射值,直接找到新的错位词集合的位置,从而将新的单词存入结果 res 中,参见代码如下:

解法一:

class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, int> m;
for (string str : strs) {
string t = str;
sort(t.begin(), t.end());
if (!m.count(t)) {
m[t] = res.size();
res.push_back({});
}
res[m[t]].push_back(str);
}
return res;
}
};

下面这种解法没有用到排序,用一个大小为 26 的 int 数组来统计每个单词中字符出现的次数,然后将 int 数组转为一个唯一的字符串,跟字符串数组进行映射,这样就不用给字符串排序了,参见代码如下:

解法二:

class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, vector<string>> m;
for (string str : strs) {
vector<int> cnt();
string t;
for (char c : str) ++cnt[c - 'a'];
for (int i = ; i < ; ++i) {
if (cnt[i] == ) continue;
t += string(, i + 'a') + to_string(cnt[i]);
}
m[t].push_back(str);
}
for (auto a : m) {
res.push_back(a.second);
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/49

类似题目:

Valid Anagram

Group Shifted Strings

参考资料:

https://leetcode.com/problems/group-anagrams/

https://leetcode.com/problems/group-anagrams/discuss/19176/share-my-short-java-solution

https://leetcode.com/problems/group-anagrams/discuss/19200/10-lines-76ms-easy-c-solution-updated-function-signature

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Group Anagrams 群组错位词的更多相关文章

  1. Group Anagrams 群组错位词

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  2. linux用户和群组

    1.用户的主要群组和次要群组   切换用户:su -username 查看群组:#vi /etc/passwd         //主要群组                  #vi /etc/gro ...

  3. [LeetCode] Anagrams 错位词

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  4. LeetCode 49: 字母异位词分组 Group Anagrams

    LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...

  5. [LeetCode] 49. Group Anagrams 分组变位词

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  6. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  7. [leetcode]49. Group Anagrams变位词归类

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  8. LeetCode OJ:Group Anagrams(同字符字符群)

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  9. [LeetCode] Positions of Large Groups 大群组的位置

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

随机推荐

  1. 动态规划-最长上升子序列(LIS)

    时间复杂度为〇(nlogn)的算法,下面就来看看. 我们再举一个例子:有以下序列A[]=3 1 2 6 4 5 10 7,求LIS长度. 我们定义一个B[i]来储存可能的排序序列,len为LIS长度. ...

  2. pygame-KidsCanCode系列jumpy-part10-角色动画(上)

    上一节学习如何利用spritesheet加载图片,但是player仍然是一张静态的图片,比较枯燥,我们要让它动起来! Player类,先把各种状态的图片加载起来: # 加载各种状态的图片序列 def ...

  3. 【linux】linux下准确查询正在tomcat下运行的java进程。准确获取正在运行的java进程的PID

    准确获取定位到tomcat下正在运行的java进程的PID命令: ps -ef|grep java | grep catalina | awk '{print $2}' 准确定位到tomcat下正在运 ...

  4. APP中的图片如何长按可以下载并保存图片到相册

    直接上图 方式一: 实现方式二: 方式三:

  5. docker_天兔

    Docker学习教程之Lepus部署(MySQL监控) 介绍 Lepus是一个由Python+PHP开发的数据库企业级监控系统,可用于MySQL/Oracle/MongoDB/Redis 下载镜像do ...

  6. Centos如何安装163yum源

    如果Centos使用系统自带的yum源,在用yum进行安装的时候,速度会受到限制,所以我们需要替换为国内的yum源,一般会选择163源,速度比较快包也比较全,使用yum进行安装的时候可以大大节省时间 ...

  7. angularjs自定义filter

    angular.Module API Overview Methods info([info]); provider(name, providerType); factory(name, provid ...

  8. Autohotkey window 下宏键盘、宏命令开发入门

  9. fiddler的编程文章

    https://www.cnblogs.com/trevan/p/9487223.html https://www.cnblogs.com/rufus-hua/p/5275980.html https ...

  10. python:print输出内容大拼接,重新认识 + 和 ,

    加号 + 和 逗号, 都可以用来拼接print的输出内容,但是两者也是有区别的. 加号拼接: print ('zwf'+'wk') 1 1 结果: zwfwk 1 1 逗号拼接: print ('zw ...