[LeetCode] 49.字母异位词分组(Medium)C语言题解

题目

  • 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

示例

①示例1
  • 输入: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”],
  • 输出:
    [
    [“ate”,“eat”,“tea”],
    [“nat”,“tan”],
    [“bat”]
    ]

说明

所有输入均为小写字母。
不考虑答案输出的顺序。

①数据范围(自测)
  • 0 <= strlen(strs[i]) < 24
②相关话题
  • 哈希表
  • 字符串
  • 排序
  • 数组
③相似题目
④题目地址

解题方法

①暴力解法
  • 当且仅当它们的字符计数(每个字符的出现次数)相同时,两个字符串是字母异位词。
  • 所以将每个字符串的字符计数用 数组模拟的哈希表 保存,暴力比较不同字符串是否是 字母异位词。
  • 时间复杂度:O(N^3)。
  • 空间复杂度:O(N^2)。
②排序 + 哈希表
  • 因为键值是字符串,所以这里的哈希表使用 uthash
  • uthash 是一个用 C 语言编写的开源库,使用宏实现了哈希表的增删改查等功能。
  • 当且仅当它们的排序字符串相等时,两个字符串是字母异位词。所以排序后的字符串可以作为哈希表的键值,并且增加变量 value1 记录键值所在行(用于分组)、变量 value2 记录键值所在行的当前列数。
  • 然后检查排序后的字符串是否存在于哈希表中,若不存在则添加键值并输出(新行第一列),若存在则将排序前的字符串输出到键值所对应的行中、并且更新所在行的当前列数。
  • 时间复杂度:O(N^2logn)。
  • 空间复杂度:O(N^2)。
②字符串 + 哈希表
  • 因为键值是字符串,所以这里的哈希表使用 uthash
  • 当且仅当它们的字符计数(每个字符的出现次数)相同时,两个字符串是字母异位词。
  • 我们可以将每个字符串 s 进行字符计数,由 26 个非负整数组成,表示 a、b、c 的数量等,并且使用 “#” 分割 26 个整数,得到的新字符串作为哈希表的键值,并且增加变量 value1 记录键值所在行(用于分组)、变量 value2 记录键值所在行的当前列数。
  • 然后检查新字符串是否存在于哈希表中,若不存在则添加键值并输出(新行第一列),若存在则将 s 输出到键值所对应的行中、并且更新所在行的当前列数。
  • 时间复杂度:O(N^2)。
  • 空间复杂度:O(N^2)。

代码详解

  • 暴力解法
char*** groupAnagrams(char** strs, int strsSize, int** columnSizes, int* returnSize) {
    char*** result = malloc(sizeof(char**)*strsSize);
    int** hash = malloc(sizeof(int*)*strsSize);
    int* flag = malloc(sizeof(int)*strsSize);
    int col = 0, row = 0;
    // 记录输出数组每行的列数。
    *columnSizes = malloc(sizeof(int)*strsSize);
    *returnSize = 0;
    
    for (int i = 0; i < strsSize; i++) {
        int len = strlen(strs[i]);
        hash[i] = malloc(sizeof(int)*26);
        // flag数组记录单词是否已经分组。
        flag[i] = 0;
        
        // 用哈希表分别记录每个单词中不同字母的出现次数。
        for (int j = 0; j < len; j++)
            hash[i][strs[i][j]-'a']++;    
    }
    for (int i = 0; i < strsSize; i++) {
        // 确定新行字符串,然后寻找它的字母异位词。
        if (flag[i] == 0) {
            result[row] = malloc(sizeof(char*)*strsSize);
            result[row][col] = malloc(sizeof(char)*24);
            flag[i] = 1;
            strcpy(result[row][col], strs[i]);
            col++;
            (*returnSize)++;
        
            // 在未分组的字符串中寻找新行字符串的字母异位词。
            for (int j = i+1; j < strsSize; j++) {
                if (flag[j] == 0) {
                    for (int k = 0; k < 26; k++) {
                        if (hash[i][k] != hash[j][k])
                            break;
                        if (k == 25) {
                            result[row][col] = malloc(sizeof(char)*24);
                            flag[j] = 1;
                            strcpy(result[row][col], strs[j]);
                            col++;
                        }
                    }
                }
            }

            (*columnSizes)[row] = col;
            // 换行。
            row++;
            col = 0;
        }
    }
    
    return result;
}
  • 排序 + 哈希表(uthash)
// 快排函数(升序)。
int compare(const void* a, const void* b) {
    return (*(char*)a)-(*(char*)b);
}

char*** groupAnagrams(char** strs, int strsSize, int** columnSizes, int* returnSize) {
    struct hash {
        char key[24];
        // 行。
        int value1;
        // 列。
        int value2;
        UT_hash_handle hh; 
    };
    
    struct hash *hashTable = NULL;    
    char*** result = malloc(sizeof(char**)*strsSize);
    int row = 0;
    // 记录输出数组每行的列数。
    *columnSizes = malloc(sizeof(int)*strsSize);
    
    for (int i = 0; i < strsSize; i++) {
        struct hash *h;
        char s[24] = "";
        strcpy(s, strs[i]);
        qsort(s, strlen(s), sizeof(char), compare);
        HASH_FIND_STR(hashTable, s, h);    
        
        // 确定新行字符串,并用哈希表记录。
        if (!h) {
            h = malloc(sizeof(struct hash));
            strcpy(h->key, s);
            h->value1 = row;
            h->value2 = 1;
            HASH_ADD_STR(hashTable, key, h);
            
            result[row] = malloc(sizeof(char*)*strsSize);
            result[row][0] = malloc(sizeof(char)*24);
            strcpy(result[row][0], strs[i]);
            row++;
        }
        // 添加到它的字母异位词所在的行。
        else {
            result[h->value1][h->value2] = malloc(sizeof(char)*24);
            strcpy(result[h->value1][h->value2], strs[i]);
            h->value2++;
        }
    }

    *returnSize = row;

    // 遍历哈希表。
    for (struct hash *s = hashTable; s!= NULL; s = s->hh.next)
        (*columnSizes)[s->value1] = s->value2;        
    
    return result;
}
  • 字符串 + 哈希表(uthash)
char*** groupAnagrams(char** strs, int strsSize, int** columnSizes, int* returnSize) {
    struct hash {
        char key[60];
        // 行。
        int value1;
        // 列。
        int value2;
        UT_hash_handle hh; 
    };
    
    struct hash *hashTable = NULL;    
    char*** result = malloc(sizeof(char**)*strsSize);
    int row = 0;
    // 记录输出数组每行的列数。
    *columnSizes = malloc(sizeof(int)*strsSize);

    for (int i = 0; i < strsSize; i++) {
        int hash[26] = {0}, len = strlen(strs[i]);
        char s[60] = "";
        
        // 对每个字符串进行字符计数,数字间用"#"分割。
        for (int j = 0; j < len; j++) 
            hash[strs[i][j]-'a']++;
        for (int j = 0; j < 26; j++) {
            char s1[2];
            s1[0] = hash[j];
            s1[1] = '\0';
            strcat(s, s1);
            strcat(s, "#");
        }
        
        struct hash *h;
        HASH_FIND_STR(hashTable, s, h);    
        
        // 将计数结果当作键值保存,字母异位词的计数结果相同。
        if (!h) {
            h = malloc(sizeof(struct hash));
            strcpy(h->key, s);
            h->value1 = row;
            h->value2 = 1;
            HASH_ADD_STR(hashTable, key, h);
            
            result[row] = malloc(sizeof(char*)*strsSize);
            result[row][0] = malloc(sizeof(char)*24);
            strcpy(result[row][0], strs[i]);
            row++;
        }
        // 添加到它的字母异位词所在的行。
        else {
            result[h->value1][h->value2] = malloc(sizeof(char)*24);
            strcpy(result[h->value1][h->value2], strs[i]);
            h->value2++;
        }
    }

    *returnSize = row;

    // 遍历哈希表。
    for (struct hash *s = hashTable; s!= NULL; s = s->hh.next)
        (*columnSizes)[s->value1] = s->value2;        
    
    return result;
}

附录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值