【力扣 - 字母异位词分组】

题目描述

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

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

示例 1:

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

示例 2:

输入: strs = [""]
输出: [[""]]

示例 3:

输入: strs = ["a"]
输出: [["a"]]

提示:

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

题解

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */

#define STR_SIZE 101

// Define a structure for the hash node
typedef struct Node {
    char str[STR_SIZE]; // Key is the string
    int row;            // Value is the row where the result is located
    struct Node *next;
} HashNode;

// Function to calculate the hash value for a string
int hash(char *str, int size) {
    long h = 0;
    for (int i = 0; i < strlen(str); i++) {
        h = (h * 26 % size + str[i] - 'a') % size; // String's hashcode, weight is 26 for lowercase letters
        // Taking modulo to prevent integer overflow
    }
    return h % size;
}

// Function to check if a string is already in the hashtable
bool contain(HashNode *hashtable, char *str, int size) {
    HashNode *head = &hashtable[hash(str, size)];
    HashNode *tail = head->next;
    while (tail) {
        if (strcmp(tail->str, str) == 0) return true;
        tail = tail->next;
    }
    return false;
}

// Function to add a string to the hashtable
void add(HashNode *hashtable, char *str, int size, int row) {
    if (contain(hashtable, str, size)) return;
    HashNode *head = &hashtable[hash(str, size)];
    // Insert at the head of the linked list
    HashNode *q = malloc(sizeof(HashNode));
    strcpy(q->str, str);
    q->row = row;
    q->next = head->next;
    head->next = q;
}

// Function to get the row of a string in the hashtable
int getRows(HashNode *hashtable, char *str, int size) {
    HashNode *head = &hashtable[hash(str, size)];
    HashNode *tail = head->next;
    while (tail) {
        if (strcmp(tail->str, str) == 0) return tail->row;
        tail = tail->next;
    }
    return -1;
}

// Comparison function for qsort
int cmp(const void *a, const void *b) {
    return *(char *)a - *(char *)b;
}

// Main function to group anagrams
char ***groupAnagrams(char **strs, int strsSize, int *retSize, int **columnSizes) {
    if (strsSize == 0 || strs == NULL) {
        *retSize = 0;
        return NULL;
    }

    HashNode *hashtable = malloc(sizeof(HashNode) * strsSize);
    memset(hashtable, 0, sizeof(HashNode) * strsSize);
    char ***ans = malloc(sizeof(char **) * strsSize);
    *retSize = 0;
    *columnSizes = malloc(sizeof(int) * strsSize);

    for (int i = 0; i < strsSize; i++) {
        char curStr[STR_SIZE] = "";
        strcpy(curStr, strs[i]);
        int lenStr = strlen(curStr);
        qsort(curStr, lenStr, sizeof(char), cmp); // Sort the string

        if (contain(hashtable, curStr, strsSize)) { // Key already exists
            int row = getRows(hashtable, curStr, strsSize); // Get the row where the key's result is located
            int col = (*columnSizes)[row];
            ans[row][col] = malloc(sizeof(char) * (lenStr + 1));
            strcpy(ans[row][col], strs[i]);
            (*columnSizes)[row]++;
        } else { // Key does not exist
            add(hashtable, curStr, strsSize, *retSize); // Insert into the hashtable
            ans[*retSize] = malloc(sizeof(char *) * strsSize); // Allocate a new row
            ans[*retSize][0] = malloc(sizeof(char) * (lenStr + 1));
            strcpy(ans[*retSize][0], strs[i]);
            (*columnSizes)[*retSize] = 1;
            (*retSize)++;
        }
    }

    return ans;
}
  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

六月悉茗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值