LeetCode 题解工作台

元音拼写检查器

在给定单词列表 wordlist 的情况下,我们希望实现一个拼写检查器,将查询单词转换为正确的单词。 对于给定的查询单词 query ,拼写检查器将会处理两类拼写错误: 大小写:如果查询匹配单词列表中的某个单词( 不区分大小写 ),则返回的正确单词与单词列表中的大小写相同。 例如: wordlist…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们遍历 ,将单词按照大小写不敏感、元音不敏感的规则分别存入哈希表 和 中,其中 的键为单词的小写形式, 的键为将单词的元音字母替换为 `*` 后的字符串,值为单词本身。用哈希表 存储 中的单词。 遍历 ,对于每个单词 ,如果 在 中,说明 在 中,直接将 加入答案数组 中。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

在给定单词列表 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] 只包含英文字母
lightbulb

解题思路

方法一:哈希表

我们遍历 wordlist\textit{wordlist},将单词按照大小写不敏感、元音不敏感的规则分别存入哈希表 low\textit{low}pat\textit{pat} 中,其中 low\textit{low} 的键为单词的小写形式,pat\textit{pat} 的键为将单词的元音字母替换为 * 后的字符串,值为单词本身。用哈希表 s\textit{s} 存储 wordlist\textit{wordlist} 中的单词。

遍历 queries\textit{queries},对于每个单词 q\textit{q},如果 q\textit{q}s\textit{s} 中,说明 q\textit{q}wordlist\textit{wordlist} 中,直接将 q\textit{q} 加入答案数组 ans\textit{ans} 中。

否则,如果 q\textit{q} 的小写形式在 low\textit{low} 中,说明 q\textit{q}wordlist\textit{wordlist} 中,且大小写不敏感,将 low[q.lower()]\textit{low}[q.\text{lower}()] 加入答案数组 ans\textit{ans} 中。

否则,如果将 q\textit{q} 的元音字母替换为 * 后的字符串在 pat\textit{pat} 中,说明 q\textit{q}wordlist\textit{wordlist} 中,且元音不敏感,将 pat[f(q)]\textit{pat}[f(q)] 加入答案数组 ans\textit{ans} 中。

否则,说明 q\textit{q}wordlist\textit{wordlist} 中,且大小写和元音都不敏感,将空字符串加入答案数组 ans\textit{ans} 中。

最后返回答案数组 ans\textit{ans} 即可。

时间复杂度 O(n+m)O(n + m),空间复杂度 O(n)O(n)。其中 nnmm 分别为 wordlist\textit{wordlist}queries\textit{queries} 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
    def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
        def f(w):
            t = []
            for c in w:
                t.append("*" if c in "aeiou" else c)
            return "".join(t)

        s = set(wordlist)
        low, pat = {}, {}
        for w in wordlist:
            t = w.lower()
            low.setdefault(t, w)
            pat.setdefault(f(t), w)

        ans = []
        for q in queries:
            if q in s:
                ans.append(q)
                continue
            q = q.lower()
            if q in low:
                ans.append(low[q])
                continue
            q = f(q)
            if q in pat:
                ans.append(pat[q])
                continue
            ans.append("")
        return ans
speed

复杂度分析

指标
时间complexity is O(C) where C is the combined length of all words and queries since each word is processed into multiple hash maps. Space complexity is O(C) for storing sets and maps for exact, case-insensitive, and vowel-error lookups.
空间O(\mathcal{C})
psychology

面试官常问的追问

外企场景
  • question_mark

    Ask how to efficiently handle multiple spelling error types in a single pass.

  • question_mark

    Probe whether candidate normalization or direct hash lookup is used for vowel-insensitive matches.

  • question_mark

    Check understanding of precedence rules between exact, case-insensitive, and vowel-error matches.

warning

常见陷阱

外企场景
  • error

    Forgetting to respect the precedence order, returning a vowel-error match before an exact match.

  • error

    Overwriting earlier wordlist entries when building maps, causing incorrect first-match results.

  • error

    Incorrectly normalizing vowels, leading to missed matches in the vowel-error step.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allow queries to include punctuation and handle normalization beyond vowels.

  • arrow_right_alt

    Handle very large wordlists with streaming or disk-based lookup instead of full hash maps.

  • arrow_right_alt

    Extend vowel-error matching to include consonant errors or phonetic similarity scoring.

help

常见问题

外企场景

元音拼写检查器题解:数组·哈希·扫描 | LeetCode #966 中等