Leetcode 49 - Group Anagrams(hash)

本文介绍了一种高效的算法来解决将字符串数组中的同字母异序词进行分组的问题。通过使用排序后的字符串作为键值,将原始字符串映射到相同的分类中,实现了O(n)的时间复杂度。

题意

给一组字符串,要求将里面的anagram分组。

样例:["eat", "tea", "tan", "ate", "nat", "bat"]

思路

暴力的思路
算法
  1. 设置一个vector<vector<int>> v,第二维大小为26,代表的一个字符串中各个字母出现的次数。
  2. 对于每个新加入的str,遍历v,看是否存在已经满足的映射,如果没有,就在v里面建立新的映射。如果有,找到v里面对应的位置,并且在ans里相应的位置上插入str。
图解

23

复杂度
  1. 遍历字符串数组, O(n)
  2. 统计该字符串的映射和遍历映射,$O(n)
  3. 检查映射, O(26)

时间复杂度: O(N2)

正解
算法

初始化一个unordered_map<string, multiset<string>> m的映射

  1. 遍历字符串数组,取得字符串s,将s排序成为t,建立m[t] = s的映射(一对多)
  2. 对于m[t]中的所有元素,成为一个分类。
复杂度

O(n)

代码

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, multiset<string>> m;
        for (auto s : strs) {
            string t = s;
            sort(t.begin(), t.end());
            m[t].insert(s);
        }
        vector<vector<string>> ans;
        for (auto x : m) {
            vector<string> tmp(x.second.begin(), x.second.end());
            ans.push_back(tmp);
        }
        return ans;
    }
};
这是一个用C语言实现的LeetCode题目:Group Anagrams。 题目描述: 给定一个字符串数组,将其分组,使得包含相同字符的字符串在同一组中。返回结果应按照字母顺序排列。 示例: 输入: ["eat", "tea", "tan", "ate", "nat", "bat"] 输出: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] 解题思路: - 遍历字符串数组,将每个字符串转换为字符数组并排序。 - 使用哈希表将排序后相同的字符串分组。 - 遍历哈希表,将每个分组的字符串数组添加到结果数组中。 代码实现: /** * Note: The returned array must be malloced, assume caller calls free(). */ char *** groupAnagrams(char ** strs, int strsSize, int* returnSize, int** returnColumnSizes){ char ***res = (char ***)malloc(sizeof(char **) * strsSize); *returnSize = 0; *returnColumnSizes = (int *)malloc(sizeof(int) * strsSize); int cnt[26] = {0}; int index = 0; struct Node *hashTable[strsSize]; memset(hashTable, 0, sizeof(hashTable)); for (int i = 0; i < strsSize; i++) { memset(cnt, 0, sizeof(cnt)); for (int j = 0; j < strlen(strs[i]); j++) { cnt[strs[i][j] - 'a']++; } char *key = (char *)malloc(sizeof(char) * 27); int idx = 0; for (int j = 0; j < 26; j++) { if (cnt[j] != 0) { key[idx++] = j + 'a'; key[idx++] = cnt[j] + '0'; } } key[idx] = '\0'; int hash = getHash(key); if (hashTable[hash] == NULL) { struct Node *node = (struct Node *)malloc(sizeof(struct Node)); node->key = key; node->next = NULL; node->strings = (char **)malloc(sizeof(char *)); node->strings[0] = strs[i]; node->count = 1; hashTable[hash] = node; } else { struct Node *p = hashTable[hash]; while (p != NULL) { if (strcmp(p->key, key) == 0) { p->count++; p->strings = (char **)realloc(p->strings, sizeof(char *) * p->count); p->strings[p->count - 1] = strs[i]; break; } p = p->next; } if (p == NULL) { struct Node *node = (struct Node *)malloc(sizeof(struct Node)); node->key = key; node->next = hashTable[hash]; node->strings = (char **)malloc(sizeof(char *)); node->strings[0] = strs[i]; node->count = 1; hashTable[hash] = node; } } } for (int i = 0; i < strsSize; i++) { if (hashTable[i] != NULL) { struct Node *p = hashTable[i]; while (p != NULL) { res[index] = p->strings; (*returnColumnSizes)[index] = p->count; (*returnSize)++; p = p->next; index++; } } } return res; } int getHash(char *key) { int hash = 0; for (int i = 0; i < strlen(key); i++) { hash = (hash * 31 + key[i]) % 100000; } return hash; } struct Node { char *key; struct Node *next; char **strings; int count; };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值