966.元音拼写检查器

元音拼写检查器

给定单词列表 wordList 和查询列表 queries,对每个查询返回正确的拼写结果(按优先级:精确匹配、忽略大小写匹配、元音替换匹配)。

示例 1:

输入:wordList = [“KiTe”,”kite”,”hare”,”Hare”], queries = [“kite”,”Kite”,”KiTe”]
输出:[“kite”,”kite”,”KiTe”]

提示:

  • 1 <= wordList.length <= 5000
  • 1 <= queries.length <= 5000
  • wordList[i] 和 queries[j] 仅包含小写字母

解析

使用三个哈希表分别存储:精确匹配、大小写忽略匹配(转为星号)、元音替换匹配。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var spellchecker = function (wordlist, queries) {
const exact = new Set(wordlist);
const lowerMap = {};
const vowelMap = {};

const toVowel = (s) => s.replace(/[aeiou]/g, '*');

for (const word of wordlist) {
const lower = word.toLowerCase();
if (!lowerMap[lower]) lowerMap[lower] = word;
const vowel = toVowel(lower);
if (!vowelMap[vowel]) vowelMap[vowel] = word;
}

return queries.map(q => {
if (exact.has(q)) return q;
const lower = q.toLowerCase();
if (lowerMap[lower]) return lowerMap[lower];
const vowel = toVowel(lower);
if (vowelMap[vowel]) return vowelMap[vowel];
return '';
});
};

时间复杂度 O(N + M * L),空间复杂度 O(N)。


966.元音拼写检查器
https://leetcode.lz5z.com/966.vowel-spellchecker/
作者
tickli
发布于
2025年3月8日
许可协议