Given an array of strings, group anagrams together.

Example:

  1. Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
  2. Output:
  3. [
  4. ["ate","eat","tea"],
  5. ["nat","tan"],
  6. ["bat"]
  7. ]

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 中,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. vector<vector<string>> groupAnagrams(vector<string>& strs) {
  4. vector<vector<string>> res;
  5. unordered_map<string, int> m;
  6. for (string str : strs) {
  7. string t = str;
  8. sort(t.begin(), t.end());
  9. if (!m.count(t)) {
  10. m[t] = res.size();
  11. res.push_back({});
  12. }
  13. res[m[t]].push_back(str);
  14. }
  15. return res;
  16. }
  17. };

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

解法二:

  1. class Solution {
  2. public:
  3. vector<vector<string>> groupAnagrams(vector<string>& strs) {
  4. vector<vector<string>> res;
  5. unordered_map<string, vector<string>> m;
  6. for (string str : strs) {
  7. vector<int> cnt();
  8. string t;
  9. for (char c : str) ++cnt[c - 'a'];
  10. for (int i = ; i < ; ++i) {
  11. if (cnt[i] == ) continue;
  12. t += string(, i + 'a') + to_string(cnt[i]);
  13. }
  14. m[t].push_back(str);
  15. }
  16. for (auto a : m) {
  17. res.push_back(a.second);
  18. }
  19. return res;
  20. }
  21. };

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. [原创]Burp Suite web应用程序渗透测试神器

    [原创]Burp Suite web应用程序渗透测试神器 一 Burp Suite介绍 Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改,扫描 ...

  2. [Functional Programming] Functional JS - Pointfree Logic Functions

    Learning notes. Video. Less than: If you use 'ramda', you maybe know 'lt, gt'.. R.lt(2, 1); //=> ...

  3. 修改Electron的libcc(libchromiumcontent)源码,重新编译electron, 设置event.isTrusted为true

    VPN非常注意: 编译的过程需要使用VPN, 否者chromium的源代码无法下载, 后面会出现总总问题 Electron的编译环境, 推荐使用物理机: win10 64位 英文版, 为了避免后期出现 ...

  4. shell编程学习笔记(十一):Shell中的while/until循环

    shell中也可以实现类似java的while循环 while循环是指满足条件时,进行循环 示例: #! /bin/sh index=10 while [ $index -gt 0 ] do inde ...

  5. ionic生成全尺寸icon和splash

    http://www.jianshu.com/p/eda363eb28d3 重新添加platform --no-resources可以禁止重新生成icon和splash ionic cordova p ...

  6. 小程序学习笔记五:API

    API 小程序提供了丰富的微信原生API,可以方便的调起微信提供的能力,如获取用户信息,本地存储,支付功能等. api调用格式: 1:wx.on 开头的 API 是监听某个事件发生的API接口,接受一 ...

  7. 在Ubuntu18.04下配置hadoop集群

    服务器准备 启动hadoop最小集群的典型配置是3台服务器, 一台作为Master, NameNode, 两台作为Slave, DataNode. 操作系统使用的Ubuntu18.04 Server, ...

  8. 读吴恩达算-EM算法笔记

    最近感觉对EM算法有一点遗忘,在表述的时候,还是有一点说不清,于是重新去看了这篇<CS229 Lecture notes>笔记. 于是有了这篇小札. 关于Jensen's inequali ...

  9. Mssql Server2005中更改sa的用户名的多种方法

    mssql安装上去时默认就是sa用户,大多数用户都会一直使用sa这个用户,这样数据库就存在很大的安全问题了,如果我们能把sa用户名修改,这样安全级别又高了一层哦,下面我们来看修改sa用户名的办法.   ...

  10. 修改Dreamweaver CC 2017 代码背景颜色

    Windows系统路径: E:\Program Files\Adobe\Dreamweaver CC\www\extensions\default\LightTheme\main.less (如果用的 ...