★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-tqqwqjho-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a wordlist
, we want to implement a spellchecker that converts a query word into a correct word.git
For a given query
word, the spell checker handles two categories of spelling mistakes:github
wordlist = ["yellow"]
, query = "YellOw"
: correct = "yellow"
wordlist = ["Yellow"]
, query = "yellow"
: correct = "Yellow"
wordlist = ["yellow"]
, query = "yellow"
: correct = "yellow"
wordlist = ["YellOw"]
, query = "yollow"
: correct = "YellOw"
wordlist = ["YellOw"]
, query = "yeellow"
: correct = ""
(no match)wordlist = ["YellOw"]
, query = "yllw"
: correct = ""
(no match)In addition, the spell checker operates under the following precedence rules:api
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:app
1 <= wordlist.length <= 5000
1 <= queries.length <= 5000
1 <= wordlist[i].length <= 7
1 <= queries[i].length <= 7
wordlist
and queries
consist only of english letters.在給定單詞列表 wordlist
的狀況下,咱們但願實現一個拼寫檢查器,將查詢單詞轉換爲正確的單詞。函數
對於給定的查詢單詞 query
,拼寫檢查器將會處理兩類拼寫錯誤:spa
wordlist = ["yellow"]
, query = "YellOw"
: correct = "yellow"
wordlist = ["Yellow"]
, query = "yellow"
: correct = "Yellow"
wordlist = ["yellow"]
, query = "yellow"
: correct = "yellow"
wordlist = ["YellOw"]
, query = "yollow"
: correct = "YellOw"
wordlist = ["YellOw"]
, query = "yeellow"
: correct = ""
(無匹配項)wordlist = ["YellOw"]
, query = "yllw"
: correct = ""
(無匹配項)此外,拼寫檢查器還按照如下優先級規則操做:code
給出一些查詢 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 }