[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 }

原文地址:https://www.cnblogs.com/strengthen/p/10201510.html

时间: 2024-10-08 16:01:24

[Swift Weekly Contest 117]LeetCode966.元音拼写检查 | Vowel Spellchecker的相关文章

[Swift Weekly Contest 117]LeetCode965. 单值二叉树 | Univalued Binary Tree

A binary tree is univalued if every node in the tree has the same value. Return true if and only if the given tree is univalued. Example 1: Input: [1,1,1,1,1,null,1] Output: true Example 2: Input: [2,2,2,5,2] Output: false Note: The number of nodes i

[Swift Weekly Contest 117]LeetCode967. 具有相同连续差异的数字 | Numbers With Same Consecutive Differences

Return all non-negative integers of length N such that the absolute difference between every two consecutive digits is K. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading

Leetcode-966 Vowel Spellchecker(元音拼写检查器)

1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4 public: 5 vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) 6 { 7 vector<string> rnt; 8 set<string> ws; 9 map&

[Swift Weekly Contest 109]LeetCode933. 最近的请求次数 | Number of Recent Calls

Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t represents some time in milliseconds. Return the number of pings that have been made from 3000 milliseconds ago until now. Any ping with time in [t - 3

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array

For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such that: For every i < j, there is no k with i < k < j such that A[k] * 2 = A[i] + A[j]. Given N, return any beautiful array A.  (It is guaranteed th