[Swift Weekly Contest 117]LeetCode966.元音拼寫檢查 | Vowel Spellchecker
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=[“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=“yellow”:correct=“”(不匹配)
示例:wordlist=[“yellow”],query=“yllw”:correct=“”(不匹配)
此外,拼寫檢查程序按以下優先規則操作:
當查詢與單詞表中的單詞完全匹配(區分大小寫)時,應返回同一單詞。
當查詢將一個單詞匹配到capitalization時,您應該返回單詞列表中的第一個這樣的匹配。
當查詢將一個單詞匹配到元音錯誤時,您應該返回單詞列表中的第一個這樣的匹配。
如果查詢在單詞列表中沒有匹配項,則應返回空字符串。
給定一些查詢,返回單詞answer列表,其中answer[i]是正確的單詞for query=queries[i]。
例1:
輸入:wordlist=[“kite”,“kite”,“hare”,“hare”],querys=[“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
- 單詞表和查詢中的所有字符串只包含英文字母。
536ms
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 }
[Swift Weekly Contest 117]LeetCode966.元音拼寫檢查 | Vowel Spellchecker