zoukankan      html  css  js  c++  java
  • [LeetCode] 49. 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(26);
                string t;
                for (char c : str) ++cnt[c - 'a'];
                for (int i = 0; i < 26; ++i) {
                    if (cnt[i] == 0) continue;
                    t += string(1, 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 题目讲解汇总(持续更新中...)

  • 相关阅读:
    【USACO18JAN】MooTube G
    【JSOI2008】星球大战
    【ECF#87】小结
    【NOIP2013】火柴排队
    【USACO04OPEN】MooFest G
    【NOI OL #2】T3 游戏
    【NOI OL #2】T2 子序列问题
    简单NLT
    python中的位运算
    列表和元组
  • 原文地址:https://www.cnblogs.com/grandyang/p/4385822.html
Copyright © 2011-2022 走看看