★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10201510.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a wordlist
, we want to implement a spellchecker that converts a query word into a correct word.
For a given query
word, the spell checker handles two categories of spelling mistakes:
- Capitalization: If the query matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the case in the wordlist.
- Example:
wordlist = ["yellow"]
,query = "YellOw"
:correct = "yellow"
- Example:
wordlist = ["Yellow"]
,query = "yellow"
:correct = "Yellow"
- Example:
wordlist = ["yellow"]
,query = "yellow"
:correct = "yellow"
- Example:
- Vowel Errors: If after replacing the vowels ('a', 'e', 'i', 'o', 'u') of the query word with any vowel individually, it matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the match in the wordlist.
- Example:
wordlist = ["YellOw"]
,query = "yollow"
:correct = "YellOw"
- Example:
wordlist = ["YellOw"]
,query = "yeellow"
:correct = ""
(no match) - Example:
wordlist = ["YellOw"]
,query = "yllw"
:correct = ""
(no match)
- Example:
In addition, the spell checker operates under the following precedence rules:
- When the query exactly matches a word in the wordlist (case-sensitive), you should return the same word back.
- When the query matches a word up to capitlization, you should return the first such match in the wordlist.
- When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
- If the query has no matches in the wordlist, you should return the empty string.
Given some queries
, return a list of words answer
, where answer[i]
is the correct word for query = queries[i]
.
Example 1:
Input: wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
Output: ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
Note:
1 <= wordlist.length <= 5000
1 <= queries.length <= 5000
1 <= wordlist[i].length <= 7
1 <= queries[i].length <= 7
- All strings in
wordlist
andqueries
consist only of english letters.
在给定单词列表 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]
得到的正确单词。
示例:
输入:wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"] 输出:["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
提示:
1 <= wordlist.length <= 5000
1 <= queries.length <= 5000
1 <= wordlist[i].length <= 7
1 <= queries[i].length <= 7
wordlist
和queries
中的所有字符串仅由英文字母组成。
476ms
1 class Solution { 2 func spellchecker(_ wordlist: [String], _ queries: [String]) -> [String] { 3 var ori:[String:String] = [String:String]() 4 var lowerCase:[String:String] = [String:String]() 5 var vowel:[String:String] = [String:String]() 6 7 for i in 0..<wordlist.count 8 { 9 ori[wordlist[i]] = wordlist[i] 10 var lower:String = wordlist[i].lowercased() 11 if lowerCase[lower] == nil 12 { 13 lowerCase[lower] = wordlist[i] 14 } 15 16 var vowelString:String = changeVowel(wordlist[i]) 17 if vowel[vowelString] == nil 18 { 19 vowel[vowelString] = wordlist[i] 20 } 21 } 22 23 var ans:[String] = [String](repeating:String(),count:queries.count) 24 for i in 0..<queries.count 25 { 26 if ori[queries[i]] != nil 27 { 28 ans[i] = ori[queries[i]]! 29 } 30 else if lowerCase[queries[i].lowercased()] != nil 31 { 32 ans[i] = lowerCase[queries[i].lowercased()]! 33 } 34 else if vowel[changeVowel(queries[i])] != nil 35 { 36 ans[i] = vowel[changeVowel(queries[i])]! 37 } 38 else 39 { 40 ans[i] = String() 41 } 42 } 43 return ans 44 } 45 46 func changeVowel(_ s:String) -> String 47 { 48 var str:String = String() 49 var s = s.lowercased() 50 var vowels:Set<Character> = ["a","e","i","o","u"] 51 for i in 0..<s.count 52 { 53 var char:Character = s[i] 54 if vowels.contains(char) 55 { 56 str.append("a") 57 } 58 else 59 { 60 str.append(char) 61 } 62 } 63 return str 64 } 65 } 66 67 extension String { 68 //subscript函数可以检索数组中的值 69 //直接按照索引方式截取指定索引的字符 70 subscript (_ i: Int) -> Character { 71 //读取字符 72 get {return self[index(startIndex, offsetBy: i)]} 73 } 74 }
608ms
1 class Solution { 2 private func toVowelStr(_ str: String) -> String { 3 let vowelChars = Set<Character>(["A","E","I","O","U"]) 4 let chars1 = Array(str.uppercased()) 5 var dstChars = [Character]() 6 for c in chars1 { 7 if vowelChars.contains(c) { 8 dstChars.append("A") 9 }else { 10 dstChars.append(c) 11 } 12 } 13 return String(dstChars) 14 } 15 16 func spellchecker(_ wordlist: [String], _ queries: [String]) -> [String] { 17 var ans = [String]() 18 var mapWordlist = [String: Bool]() 19 var mapUpperWordlist = [String: String]() 20 var mapvowelWordlist = [String: String]() 21 wordlist.forEach { (str) in 22 mapWordlist[str] = true 23 let strs = mapUpperWordlist[str.uppercased()] 24 if strs == nil { 25 mapUpperWordlist[str.uppercased()] = str 26 } 27 let vowelStr = toVowelStr(str) 28 let strs2 = mapvowelWordlist[vowelStr] 29 if strs2 == nil { 30 mapvowelWordlist[vowelStr] = str 31 } 32 } 33 34 for q in queries { 35 let exist = mapWordlist[q] ?? false 36 if exist { 37 ans.append(q) 38 continue 39 } 40 41 let strs = mapUpperWordlist[q.uppercased()] 42 if strs != nil { 43 ans.append(strs!) 44 continue 45 } 46 47 let vowelStrMap = mapvowelWordlist[toVowelStr(q)] 48 if vowelStrMap != nil { 49 ans.append(vowelStrMap!) 50 continue 51 } 52 ans.append("") 53 } 54 return ans 55 } 56 }
716ms
1 extension Optional { 2 func orElse(_ defaultElement: Wrapped) -> Wrapped { 3 return self ?? defaultElement 4 } 5 } 6 7 extension Sequence where Self.Element: Hashable { 8 var frequencies: [Self.Element: Int] { 9 var map: [Self.Element: Int] = [:] 10 forEach { (element) in 11 map[element] = map[element].orElse(0) + 1 12 } 13 return map 14 } 15 } 16 17 class Solution { 18 func spellchecker(_ wordlist: [String], _ queries: [String]) -> [String] { 19 var exactMap: [String: [String]] = [:] 20 var capMap: [String: [String]] = [:] 21 var vowelMap: [String: [String]] = [:] 22 var res: [String] = [] 23 24 for word in wordlist { 25 exactMap[word] = exactMap[word].orElse([]) + [word] 26 capMap[word.lowercased()] = capMap[word.lowercased()].orElse([]) + [word] 27 vowelMap[conv(word.lowercased())] = vowelMap[conv(word.lowercased())].orElse([]) + [word] 28 } 29 30 for query in queries { 31 if let words = exactMap[query], let word = words.first { 32 res.append(word) 33 } else if let words = capMap[query.lowercased()], let word = words.first { 34 res.append(word) 35 } else if let words = vowelMap[conv(query.lowercased())], let word = words.first { 36 res.append(word) 37 } else { 38 res.append("") 39 } 40 } 41 42 return res 43 } 44 45 func conv(_ s: String) -> String { 46 var res: [Character] = Array(s) 47 48 for (i, symbol) in s.enumerated() { 49 if symbol == "a" || symbol == "e" || symbol == "i" || symbol == "o" || symbol == "u" { 50 res[i] = "1" 51 } 52 } 53 54 return String(res) 55 } 56 }
732ms
1 class Solution { 2 func spellchecker(_ wordlist: [String], _ queries: [String]) -> [String] { 3 var exactMap: [String: [String]] = [:] 4 var capMap: [String: [String]] = [:] 5 var vowelMap: [String: [String]] = [:] 6 var res: [String] = [] 7 8 for word in wordlist { 9 exactMap[word] = (exactMap[word] ?? []) + [word] 10 capMap[word.lowercased()] = (capMap[word.lowercased()] ?? []) + [word] 11 vowelMap[conv(word.lowercased())] = (vowelMap[conv(word.lowercased())] ?? []) + [word] 12 } 13 14 for query in queries { 15 if let words = exactMap[query], let word = words.first { 16 res.append(word) 17 } else if let words = capMap[query.lowercased()], let word = words.first { 18 res.append(word) 19 } else if let words = vowelMap[conv(query.lowercased())], let word = words.first { 20 res.append(word) 21 } else { 22 res.append("") 23 } 24 } 25 26 return res 27 } 28 29 func conv(_ s: String) -> String { 30 var res: [Character] = Array(s) 31 32 for (i, symbol) in s.enumerated() { 33 if symbol == "a" || symbol == "e" || symbol == "i" || symbol == "o" || symbol == "u" { 34 res[i] = "1" 35 } 36 } 37 38 return String(res) 39 } 40 }