49.字母异位词分组

给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。

字母异位词 是由重新排列源单词的所有字母得到的一个新单词。

示例 1:

复制代码
输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
输出: [["bat"],["nat","tan"],["ate","eat","tea"]]

示例 2:

复制代码
输入: strs = [""]
输出: [[""]]

示例 3:

复制代码
输入: strs = ["a"]
输出: [["a"]]

提示:

  • 1 <= strs.length <= 104
  • 0 <= strs[i].length <= 100
  • strs[i] 仅包含小写字母

思路:对每个字符串排序,建立map关联容器。使用排序后的字符串当key,排序后一样的字符串放在一起。

cpp 复制代码
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string,vector<string>> map_hash;
        for(string& str:strs){
            string key = str;
            sort(key.begin(), key.end());
            map_hash[key].emplace_back(str);
        }
        vector<vector<string>> ans;
        for(auto it = map_hash.begin(); it != map_hash.end(); ++it){
            ans.emplace_back(it->second);
        }
        return ans;
    }
};

思路二:由于互为字母异位词的两个字符串包含的字母相同,因此两个字符串中的相同字母出现的次数一定是相同的,故可以将每个字母出现的次数使用字符串表示,作为哈希表的键。

cpp 复制代码
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string,vector<string>> map_hash;
        for(string& str:strs){
            array<int,26> num {0};
            int length = str.length();
            for(int i = 0; i < length; i++){
                num[str[i] - 'a']++;
            }
            string key;
            for(int i = 0; i < 26; i++){
                key = key + "#" + to_string(num[i]);
            }
            map_hash[key].emplace_back(str);
        }
        vector<vector<string>> ans;
        for(auto it = map_hash.begin(); it != map_hash.end(); ++it){
            ans.emplace_back(it->second);
        }
        return ans;
    }
};

官方代码:https://leetcode.cn/problems/group-anagrams/solutions/520469/zi-mu-yi-wei-ci-fen-zu-by-leetcode-solut-gyoc/?envType=study-plan-v2&envId=top-100-liked

cpp 复制代码
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        // 自定义对 array<int, 26> 类型的哈希函数
        auto arrayHash = [fn = hash<int>{}] (const array<int, 26>& arr) -> size_t {
            return accumulate(arr.begin(), arr.end(), 0u, [&](size_t acc, int num) {
                return (acc << 1) ^ fn(num);
            });
        };

        unordered_map<array<int, 26>, vector<string>, decltype(arrayHash)> mp(0, arrayHash);
        for (string& str: strs) {
            array<int, 26> counts{};
            int length = str.length();
            for (int i = 0; i < length; ++i) {
                counts[str[i] - 'a'] ++;
            }
            mp[counts].emplace_back(str);
        }
        vector<vector<string>> ans;
        for (auto it = mp.begin(); it != mp.end(); ++it) {
            ans.emplace_back(it->second);
        }
        return ans;
    }
};
相关推荐
若鱼19199 小时前
JPA/Hibernate中一对一关联时不持有外键方的属性延迟加载为什么不生效?
java·spring
吴声子夜歌9 小时前
ES6——Iterator和for...of循环详解
前端·javascript·es6
砍材农夫9 小时前
spring-ai 第八模型介绍-图像模型
java·人工智能·spring
小李子呢02119 小时前
前端八股3---ref和reactive
开发语言·前端·javascript
落魄江湖行9 小时前
基础篇三 Nuxt4 组件进阶:插槽与事件传递
前端·nuxt4
kerli9 小时前
Compose 组件:LazyColumn 核心参数与 key/contentType 详解
android·前端
好运的阿财9 小时前
“锟斤拷”问题——程序中用powershell执行命令出现中文乱码的解决办法
linux·前端·人工智能·机器学习·架构·编辑器·vim
踩着两条虫9 小时前
VTJ.PRO AI + 低代码实战:接入高德地图
前端·vue.js·ai编程
绝世唐门三哥9 小时前
React性能优化:memo、useMemo和useCallback全解析
前端·react.js·memo
橘子hhh9 小时前
Netty基础服务器实现
java·nio