【leetcode100-002】【hash】字母异位词分组

【题干】

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

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

【思路】

要求分组,则关键在于选择分组的依据。

想出一个可以唯一标识某一组字母组合的特征作为key,然后将特征符合该key的字母排列作为值,构建hash map并输出。

特征获取的方式可以是:

(1)排序后的字符串

(2)一个长度为26的记录每个小写字母出现次数的数组

(3)用26个质数对应26个字母后,将字符串内所有字母对应质数相乘后获得的乘积

【题解】

排序字符串

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

数组

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;
    }
};

乘积

class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
    //字母对应质数map
    unordered_map<char, int> AlphtoPrime_Map;
    //根据字母出现频率 优化了一下 高频字母对应小质数
    int primeNum[26] = {5, 71, 31, 29, 2, 53, 59, 23, 11, 89, 79, 37, 41, 13, 7, 43, 97, 17, 19, 3, 47, 73, 61, 83, 67, 101};
    for (int i = 0; i < 26; ++ i) {
        AlphtoPrime_Map[i + 'a'] = primeNum[i];
    }

    //计算字符串对应的乘积key
    unordered_map<unsigned int,vector<string>> mp;
    unsigned int key;
    for(string str:strs){
    key = 1;
    for(int index = 0 ; index < str.size() ; index++){
    key *= AlphtoPrime_Map[str[index]];
    }
    mp[key].emplace_back(str);
    }

    //
    vector<vector<string>> result;
    for(auto it=mp.begin(); it !=mp.end() ; it++){
      result.emplace_back(it->second);
    }
    return result;
}
};

【总结】

本题训练到了构建hash map时key值的选择思路,不仅可以可以考虑从原有信息中提取+计算获得特征值,还可以考虑直接用结构性的数据记录特征并当作key。

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值