题目如下:

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. NFA 、DFA 简述

    转载请注明出处 https://www.cnblogs.com/majianming/p/11823697.html 目前常见的正则表达引擎总体分为2种,DFA (确定型有穷状态自动机) 和 NFA ...

  2. 查询qq登陆状态

    function qq_status(){ if (empty($qq))$qq = 287959133; $url = 'http://wpa.qq.com/pa?p=2:'.$qq.':52'; ...

  3. Qt对话框部分学习

    一.对话框部分常用内容 颜色对话框.文件对话框.字体对话框.输入对话框.消息对话框.进度对话框.错误对话框.向导对话框. 二.代码部分   //widget.h #ifndef MYWIDGET_H ...

  4. ribbon学习

    spring cloud 中的负载均衡有ribbon和feign 引入ribbon依赖 <!--ribbon相关--> <dependency> <groupId> ...

  5. VIEW当中自定义属性的使用

    主要有三种方法可以实现自定义属性. 第一种方法,直接设置属性值,通过attrs.getAttributeResourceValue拿到这个属性值. (1)在xml文件中设置属性值 [html] vie ...

  6. 前端每日实战:71# 视频演示如何用纯 CSS 创作一个跳 8 字型舞的 loader

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gKNMMm 可交互视频 此视频是可 ...

  7. Django2.0中得url路由path得用法

    Django2.0中,url得匹配规则更新了,在django1.0中,url是用正则表达式书写得,相对来说比较繁琐一些,在django2.0中进行了升级优化,改为了path from django.u ...

  8. sql审核工具调研安装-sqlAdvisor和soar

    sql审核工具调研  基于soar的sql审核查询平台: https://github.com/beiketianzhuang/data-platform-soar 1.美团工具sqlAdvisor工 ...

  9. Java学习之单例模式

    单例设计模式:解决一个类在内存中只存在一个对象思想:1.为了避免过多创建类的对象,禁止此类以外创建对象(构造方法私有化)2.为了类可以被使用就必须创建此类对象,只好在本类中,创建对象3.在此类中创建的 ...

  10. QMUI android 框架 git下载项目运行报错解决 input String“”

    1.编译源码,input String“” 解决办法: 打开qmuidemo里面的gradle文件,注释掉顶部的 //def cmd = 'git rev-list HEAD --count'//de ...