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;
}
相关推荐
YuK.W2 小时前
Leetcode100: 94.二叉树中序遍历、104.二叉树最大深度、226.翻转二叉树
java·算法·leetcode·二叉树
想吃火锅10057 小时前
【leetcode】146.LRU缓存js
算法·leetcode·缓存
To_OC6 天前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode
To_OC6 天前
LC 208 实现 Trie 前缀树:曾被名字劝退,写完发现是送分题
javascript·算法·leetcode
To_OC7 天前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过
javascript·算法·leetcode
To_OC7 天前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode
To_OC8 天前
LC 128 最长连续序列:别上来就排序,O (n) 解法才是这题的灵魂
javascript·算法·leetcode
To_OC10 天前
LC 49 字母异位词分组:想到哈希表很简单,选对 key 才是精髓
javascript·算法·leetcode
To_OC11 天前
LC 1 两数之和:面试第一道必考题,暴力解法直接被面试官 pass
javascript·算法·leetcode
想吃火锅100517 天前
【leetcode】121.买卖股票的最佳时机js/c++
算法·leetcode·职场和发展