[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 ...
随机推荐
- Java全栈程序员之08:MAVEN+JAVA配置
从Spring3.0开始,Spring支持以Java配置的方式来代替XML配置.这一点说起来其实有点可笑,XML配置的方式最初被创建出来就是为了让配置与程序员无关.可是最终我们发现,绝大多数的那些配置 ...
- postman6 在Linux中,body和response字体显示不正常的解决方法
在Linux中,postman的body和response使用的默认字体如果没有安装的话,会导致字体和光标的位置不一致,例如字体显示长度只有30,而光标在70的位置,导致编辑困难. 经查找css的定义 ...
- 工厂模式 Factory MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Ubuntu安装WDCP遇到的无法便于错误解决方法
WDCP v3.2安装 WDCP支持CentOS系统下安装,包括了32bit或者64bit,最新版本建议在6.x以上版本使用,源码安装命令为: wget http://dl.wdlinux.cn/la ...
- 图解Windows下 GIT GUI 使用教程
https://jingyan.baidu.com/article/19020a0a7ae6af529c284248.html 本篇经验将和大家介绍Windows下 GIT GUI 使用教程,希望对大 ...
- C# System.Threading.AutoResetEvent
表示线程同步事件在一个等待线程释放后收到信号时自动重置. using System; using System.Threading; // Visual Studio: Replace the def ...
- Canvas 旋转的图片
var image = new Image(), counter = 0; image.onload = function () { var CANVAS_WIDTH = 300, CANVAS_HE ...
- phpStorm中如何不让其自动添加封闭大括号?
Settings > Editor > General->Smart Keys.
- CentOS7配置防火墙
使用命令的方式配置 ##Add firewall-cmd --permanent --zone=public --add-port=/tcp ##Remove firewall-cmd --perma ...
- MySQL 5.5主从关于‘复制过滤’的深入探究
关于MySQL主从复制的过滤,例如通过binlog-ignore-db.replicate-do-db.replicate-wild-do-table等.如果不好好研究过这些过滤选项就用的话,是有可能 ...