Leetcode每日一练--28

966. 元音拼写检查器

难度:中等

在给定单词列表 wordlist 的情况下,我们希望实现一个拼写检查器,将查询单词转换为正确的单词。

对于给定的查询单词 query,拼写检查器将会处理两类拼写错误:

  • 大小写:如果查询匹配单词列表中的某个单词(不区分大小写 ),则返回的正确单词与单词列表中的大小写相同。
    • 例如:wordlist = ["yellow"], query = "YellOw": correct = "yellow"
    • 例如:wordlist = ["Yellow"], query = "yellow": correct = "Yellow"
    • 例如:wordlist = ["yellow"], query = "yellow": correct = "yellow"
  • 元音错误:如果在将查询单词中的元音 ('a', 'e', 'i', 'o', 'u') 分别替换为任何元音后,能与单词列表中的单词匹配(不区分大小写 ),则返回的正确单词与单词列表中的匹配项大小写相同。
    • 例如:wordlist = ["YellOw"], query = "yollow": correct = "YellOw"
    • 例如:wordlist = ["YellOw"], query = "yeellow": correct = "" (无匹配项)
    • 例如:wordlist = ["YellOw"], query = "yllw": correct = "" (无匹配项)

此外,拼写检查器还按照以下优先级规则操作:

  • 当查询完全匹配单词列表中的某个单词(区分大小写)时,应返回相同的单词。
  • 当查询匹配到大小写问题的单词时,您应该返回单词列表中的第一个这样的匹配项。
  • 当查询匹配到元音错误的单词时,您应该返回单词列表中的第一个这样的匹配项。
  • 如果该查询在单词列表中没有匹配项,则应返回空字符串。

给出一些查询 queries,返回一个单词列表 answer,其中 answer[i] 是由查询 query = queries[i] 得到的正确单词。

示例 1:

复制代码
输入:wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
输出:["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]

示例 2:

复制代码
输入:wordlist = ["yellow"], queries = ["YellOw"]
输出:["yellow"]

提示:

  • 1 <= wordlist.length, queries.length <= 5000
  • 1 <= wordlist[i].length, queries[i].length <= 7
  • wordlist[i]queries[i] 只包含英文字母

代码

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define HASH_SIZE 10007

typedef struct MapNode {
    char* key;
    char* value;
    struct MapNode* next;
} MapNode;

unsigned long hash_func(const char* str) {
    unsigned long hash = 0;
    while (*str) {
        hash = (hash * 131 + (unsigned char)(*str)) % HASH_SIZE;
        str++;
    }
    return hash;
}

void set_insert(MapNode** hashtable, const char* key) {
    unsigned long idx = hash_func(key);
    MapNode* p = hashtable[idx];
    while (p) {
        if (strcmp(p->key, key) == 0) {
            return;
        }
        p = p->next;
    }
    MapNode* node = (MapNode*)malloc(sizeof(MapNode));
    node->key = strdup(key);
    node->value = NULL;
    node->next = hashtable[idx];
    hashtable[idx] = node;
}

int set_contains(MapNode** hashtable, const char* key) {
    unsigned long idx = hash_func(key);
    MapNode* p = hashtable[idx];
    while (p) {
        if (strcmp(p->key, key) == 0) {
            return 1;
        }
        p = p->next;
    }
    return 0;
}

void map_insert(MapNode** hashtable, const char* key, const char* value) {
    unsigned long idx = hash_func(key);
    MapNode* p = hashtable[idx];
    while (p) {
        if (strcmp(p->key, key) == 0) {
            return;
        }
        p = p->next;
    }
    MapNode* node = (MapNode*)malloc(sizeof(MapNode));
    node->key = strdup(key);
    node->value = strdup(value);
    node->next = hashtable[idx];
    hashtable[idx] = node;
}

char* map_get(MapNode** hashtable, const char* key) {
    unsigned long idx = hash_func(key);
    MapNode* p = hashtable[idx];
    while (p) {
        if (strcmp(p->key, key) == 0) {
            return p->value;
        }
        p = p->next;
    }
    return NULL;
}

char* tolower_string(const char* str) {
    int len = strlen(str);
    char* res = (char*)malloc(len + 1);
    for (int i = 0; i < len; i++) {
        res[i] = tolower(str[i]);
    }
    res[len] = '\0';
    return res;
}

char* replace_vowel(const char* str) {
    int len = strlen(str);
    char* res = (char*)malloc(len + 1);
    for (int i = 0; i < len; i++) {
        char c = str[i];
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            res[i] = '*';
        } else {
            res[i] = c;
        }
    }
    res[len] = '\0';
    return res;
}

void free_hashtable(MapNode** hashtable) {
    for (int i = 0; i < HASH_SIZE; i++) {
        MapNode* p = hashtable[i];
        while (p) {
            MapNode* tmp = p;
            p = p->next;
            free(tmp->key);
            if (tmp->value) free(tmp->value);
            free(tmp);
        }
    }
}

char** spellchecker(char** wordlist, int wordlistSize, char** queries, int queriesSize, int* returnSize) {
    MapNode* set_exact[HASH_SIZE] = {0};
    MapNode* map_lower[HASH_SIZE] = {0};
    MapNode* map_vowel[HASH_SIZE] = {0};
    
    for (int i = 0; i < wordlistSize; i++) {
        char* word = wordlist[i];
        if (!set_contains(set_exact, word)) {
            set_insert(set_exact, word);
        }
        
        char* lower = tolower_string(word);
        if (map_get(map_lower, lower) == NULL) {
            map_insert(map_lower, lower, word);
        }
        free(lower);
        
        char* lower2 = tolower_string(word);
        char* vow_str = replace_vowel(lower2);
        if (map_get(map_vowel, vow_str) == NULL) {
            map_insert(map_vowel, vow_str, word);
        }
        free(lower2);
        free(vow_str);
    }
    
    char** ans = (char**)malloc(queriesSize * sizeof(char*));
    *returnSize = queriesSize;
    
    for (int i = 0; i < queriesSize; i++) {
        char* q = queries[i];
        if (set_contains(set_exact, q)) {
            ans[i] = strdup(q);
        } else {
            char* q_lower = tolower_string(q);
            char* candidate = map_get(map_lower, q_lower);
            if (candidate) {
                ans[i] = strdup(candidate);
                free(q_lower);
                continue;
            }
            char* q_vow = replace_vowel(q_lower);
            candidate = map_get(map_vowel, q_vow);
            if (candidate) {
                ans[i] = strdup(candidate);
            } else {
                ans[i] = strdup("");
            }
            free(q_lower);
            free(q_vow);
        }
    }
    
    free_hashtable(set_exact);
    free_hashtable(map_lower);
    free_hashtable(map_vowel);
    
    return ans;
}
相关推荐
云里雾里!12 小时前
力扣 977. 有序数组的平方:双指针法的优雅解法
算法·leetcode·职场和发展
Dream it possible!18 小时前
LeetCode 面试经典 150_二叉搜索树_二叉搜索树中第 K 小的元素(86_230_C++_中等)
c++·leetcode·面试
sin_hielo18 小时前
leetcode 2872
数据结构·算法·leetcode
Booksort20 小时前
【LeetCode】算法技巧专题(持续更新)
算法·leetcode·职场和发展
小白程序员成长日记20 小时前
力扣每日一题 2025.11.28
算法·leetcode·职场和发展
Swift社区20 小时前
LeetCode 435 - 无重叠区间
算法·leetcode·职场和发展
sin_hielo20 小时前
leetcode 1018
算法·leetcode
橘颂TA1 天前
【剑斩OFFER】算法的暴力美学——只出现一次的数字 ||
算法·leetcode·动态规划
小欣加油1 天前
leetcode 1018 可被5整除的二进制前缀
数据结构·c++·算法·leetcode·职场和发展
无敌最俊朗@1 天前
链表-力扣hot100-随机链表的复制138
数据结构·leetcode·链表