class Solution {
public:
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
}
};
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]
只包含英文字母原站题解
golang 解法, 执行用时: 48 ms, 内存消耗: 6.9 MB, 提交时间: 2023-06-07 11:09:59
var dictMap = map[string]bool{ "a": true, "e": true, "i": true, "o": true, "u": true, } func spellchecker(wordlist []string, queries []string) []string { rets := []string{} dict := map[string]bool{} lowerDict := map[string]int{} lowerWithoutVowelDict := map[string]int{} for index, word := range wordlist { dict[word]=true word = strings.ToLower(word) _, ok := lowerDict[word] if !ok { lowerDict[word]=index } for str, _ := range dictMap { word = strings.ReplaceAll(word, str, "-") } _, ok = lowerWithoutVowelDict[word] if !ok { lowerWithoutVowelDict[word]=index } } for _, query := range queries { _, ok := dict[query] if ok { rets = append(rets, query) continue } index, ok := lowerDict[strings.ToLower(query)] if ok { rets = append(rets, wordlist[index]) continue } query := strings.ToLower(query) for str, _ := range dictMap { query = strings.ReplaceAll(query, str, "-") } index, ok = lowerWithoutVowelDict[query] if ok { rets = append(rets, wordlist[index]) continue } rets = append(rets, "") } return rets }
golang 解法, 执行用时: 40 ms, 内存消耗: 6.8 MB, 提交时间: 2023-06-07 11:09:39
func spellchecker(wordlist []string, queries []string) []string { words := make(map[string]bool) wordsToLower := make(map[string]string) wordsVowel := make(map[string]string) toVow := func(s string) string { var res strings.Builder for _, c := range s { switch c { case 'a','e','i','o','u': res.WriteByte('?') default: res.WriteRune(c) } } return res.String() } for _, w := range wordlist { words[w] = true lower := strings.ToLower(w) if _, ok := wordsToLower[lower]; !ok { wordsToLower[lower] = w } vow := toVow(lower) if _, ok := wordsVowel[vow]; !ok { wordsVowel[vow] = w } } for i, w := range queries { queries[i] = "" if words[w] { queries[i] = w continue } lower := strings.ToLower(w) if v, ok := wordsToLower[lower]; ok { queries[i] = v continue } vow := toVow(lower) if v, ok := wordsVowel[vow]; ok { queries[i] = v } } return queries }
python3 解法, 执行用时: 96 ms, 内存消耗: 18.7 MB, 提交时间: 2023-06-07 11:08:56
class Solution: def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]: n=len(wordlist) lowerWords=[word.lower() for word in wordlist] wordDic={word:i for i,word in enumerate(wordlist)} lowerWordDic={} for i,word in enumerate(lowerWords): if word not in lowerWordDic: lowerWordDic[word]=i vowDic={} vows={'e','i','o','u'} for word in lowerWordDic: index=lowerWordDic[word] for v in vows: word=word.replace(v,'a') if word not in vowDic: vowDic[word]=index res=[] for word in queries: if word in wordDic: res.append(word) continue if word.lower() in lowerWordDic: res.append(wordlist[lowerWordDic[word.lower()]]) continue word=word.lower() for v in vows: word=word.replace(v,'a') if word in vowDic: res.append(wordlist[vowDic[word]]) else: res.append('') return res
python3 解法, 执行用时: 3252 ms, 内存消耗: 18.1 MB, 提交时间: 2023-06-07 11:07:53
class Solution: def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]: ans = ['' for q in queries] lower_wordlist = [w.lower() for w in wordlist] vowel_wordlist = [re.sub(r'[a|e|i|o|u]', 'a', w) for w in lower_wordlist] for i, q in enumerate(queries): lower_q = q.lower() vowel_q = re.sub(r'[a|e|i|o|u]', 'a', lower_q) if q in wordlist: ans[i] = q elif lower_q in lower_wordlist: ans[i] = wordlist[lower_wordlist.index(lower_q)] elif vowel_q in vowel_wordlist: ans[i] = wordlist[vowel_wordlist.index(vowel_q)] return ans
python3 解法, 执行用时: 88 ms, 内存消耗: 18.8 MB, 提交时间: 2023-06-07 11:07:23
class Solution: def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]: def devowel(word: str) -> str: return ''.join('*' if c in 'aeiou' else c for c in word) words_perfect = set(wordlist) words_cap = {} words_vow = {} for word in wordlist: wordlow = word.lower() words_cap.setdefault(wordlow, word) words_vow.setdefault(devowel(wordlow), word) def solve(query: str) -> str: if query in words_perfect: return query queryL = query.lower() if queryL in words_cap: return words_cap[queryL] queryLV = devowel(queryL) if queryLV in words_vow: return words_vow[queryLV] return "" return list(map(solve, queries))