[LeetCode] Group Anagrams 群组错位词
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
类似题目:
参考资料:
https://leetcode.com/problems/group-anagrams/
https://leetcode.com/problems/group-anagrams/discuss/19176/share-my-short-java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Group Anagrams 群组错位词的更多相关文章
- Group Anagrams 群组错位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- linux用户和群组
1.用户的主要群组和次要群组 切换用户:su -username 查看群组:#vi /etc/passwd //主要群组 #vi /etc/gro ...
- [LeetCode] Anagrams 错位词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- LeetCode 49: 字母异位词分组 Group Anagrams
LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...
- [LeetCode] 49. Group Anagrams 分组变位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [leetcode]49. Group Anagrams变位词归类
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- LeetCode OJ:Group Anagrams(同字符字符群)
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- [LeetCode] Positions of Large Groups 大群组的位置
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
随机推荐
- MySQL体系结构图详解
体系结构图如下: 连接层 思想为解决资源的频繁分配﹑释放所造成的问题,为数据库连接建立一个“缓冲池”.原理预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕 ...
- 在windows命令行批量ping局域网内IP
参考了博客园Alfred Zhao的文章<Windows平台ping测试局域网所有在用IP> 在cmd命令行运行如下命令即可: ,,) -w .%i | find "回复&quo ...
- Swift 拷贝文件夹,实现文件夹内容整体复制
我们知道,在沙盒内,iOS要拷贝一个文件,可以使用 fileManager.copyItem(atPath: fullPath, toPath: fulltoPath) 方法简单实现,不过当我们要拷贝 ...
- vim编辑器里shift + 3 出现高亮问题,怎么取消掉
在编辑器里非编辑状态,输入: shift + 3 (#) shift + 8 (*) 会出现高亮显示,看着很不舒服, 取消方式: :noh :/aaa (随便的字母或数字都可)
- 树莓派进阶之路 (037) - 设置树莓派3 B+的静态IP
修改/etc/dhcpcd.conf 文件 sudo vim /etc/dhcpcd.conf interface eth0 static ip_address= static routers=192 ...
- “The subscription does not exist” when a distributor primary replica fails over to a replica that does not use the same agent profile
Symptoms Consider the following scenario: In Microsoft SQL Server 2017, you have a distribution agen ...
- (9) MySQL主主复制架构使用方法
一. 回忆主从复制的一些缺点 上节说到主从复制的一些问题 我们再来回忆一下 主从复制,增加了一个数据库副本,从数据库和主数据库的数据最终会是一致的 之所以说是最终一致,因为mysql复制是异步的,正常 ...
- Cobalt Strike DNS通讯实例
一.域名设置 如果没有域名,可以参考另一篇博客,申请Freenom免费域名,并使用DNSPod解析 链接:https://www.cnblogs.com/ssooking/p/6364639.html ...
- 快速入门 WePY 小程序【转】
一.WePY介绍 WePY 是 腾讯 参考了Vue 等框架对原生小程序进行再次封装的框架,更贴近于 MVVM 架构模式, 并支持ES6/7的一些新特性. 二.WePY 使用 1.WePY的安装或更新都 ...
- bat搜集
1. 删除文件夹下指定名称的所有子文件夹 @echo off ::设置要保留的文件夹名,多个文件夹之间用英文逗号隔开,如果包含空格或英文逗号,英文&以及其它一些特殊字符的名字,请把该完整名字用 ...