题目如下:

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中所有元素,第二个字典保存wordlist把所有单词转换成小写后的新单词,第三个字典保存wordlist中的把所有单词中元音都替换成'a'的新单词。匹配queries中的单词也是一样的顺序,首先是精确匹配,然后忽略大小写,最后是元音都替换成'a'。

代码如下:

class Solution(object):
def replaceVowel(self,v):
newv = ''
v = v.lower()
vowel = ['a', 'e', 'i', 'o', 'u']
for j in v:
if j in vowel:
newv += 'a'
else:
newv += j
return newv
def spellchecker(self, wordlist, queries):
"""
:type wordlist: List[str]
:type queries: List[str]
:rtype: List[str]
"""
dic = {}
dic_case = {}
dic_vowel = {}
for i,v in enumerate(wordlist):
if v not in dic:
dic[v] = i
if v.lower() not in dic_case:
dic_case[v.lower()] = i
newv = self.replaceVowel(v)
if newv not in dic_vowel:
dic_vowel[newv] = i
res = []
for i in queries:
if i in dic:
res.append(wordlist[dic[i]])
elif i.lower() in dic_case:
res.append(wordlist[dic_case[i.lower()]])
elif self.replaceVowel(i) in dic_vowel:
res.append(wordlist[dic_vowel[self.replaceVowel(i)]])
else:
res.append('')
return res

【leetcode】966. Vowel Spellchecker的更多相关文章

  1. 【LeetCode】966. Vowel Spellchecker 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. JVM内存模型及GC回收算法

    该篇博客主要对JVM内存模型以及GC回收算法以自己的理解和认识做以记录. 内存模型 GC垃圾回收 1.内存模型 从上图可以看出,JVM分为 方法区,虚拟机栈,本地方法栈,堆,计数器 5个区域.其中最为 ...

  2. matplotlib不显示图片

    import matplotlib.pyplot as plt 绘制好图像之后,需要使用plt.show()才会显示出图片.

  3. SQL中的DQL查询语句

    目录 1. DQL:查询语句 排序查询 聚合函数 分组查询 分页查询 2. 约束 3. 多表之间的关系 4. 范式 DQL:查询语句 1. 排序查询 语法:order by 子句 order by 排 ...

  4. SQL Wildcards 通配符

    SQL Wildcards通配符 通配符用于替换字符串中的任何其他字符. 通配符与SQL LIKE运算符一起使用.在WHERE子句中使用LIKE运算符来搜索列中的指定模式. 有两个通配符与LIKE运算 ...

  5. linux文件查找工具——locate,find

    一文件查找介绍 文件的查找就是在文件系统上查找符合条件的文件. 文件查找的方式:locate, find非实时查找也就是基于数据库查找的locate,效率特别高. 实时查找:find 二locate ...

  6. c#将数据导出到excel中

    DataTable   dt = new BLL.T_Expiry().GetAllList().Tables[0];//查询数据 string FileName = "T_Users.xl ...

  7. 理解同步/异步/阻塞/非阻塞IO区别

    5种IO模型 1.阻塞式I/O模型 阻塞I/O(blocking I/O)模型,进程调用recvfrom,其系统调用直到数据报到达且被拷贝到应用进程的缓冲区中或者发生错误才返回.进程从调用recvfr ...

  8. php-fpm.conf详细解析篇

    一:php-fpm.conf详细解析篇: pm = static (静态模式)时只需修改 max_children数值 pm = dynamic (动态模式)时只需修改其它三个数值 pm.max_ch ...

  9. String、StringBuuffer、StringBuilder三者的区别

    string String 字符串常量(final修饰,不可被继承,线程不安全),String是常量,当创建之后即不能更改,可以给多个引用共享,在做大量字符串拼接的时候效率低.(可以通过StringB ...

  10. prototype、proto和constructor 关系

    记录: 1.构造函数:如下,Foo()就是一个构造函数 function Foo(){} 2.prototype:每一个构造函数都有一个属性叫prototype,相当于一个指针,指向它的原型对象 Fo ...