[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"
  • 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)

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 and queries 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 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10201510.html