题目如下:

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 += jreturn newvdef 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] = iif v.lower() not in dic_case:dic_case[v.lower()] = inewv = self.replaceVowel(v)if newv not in dic_vowel:dic_vowel[newv] = ires = []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

转载于:https://www.cnblogs.com/seyjs/p/10202487.html

【leetcode】966. Vowel Spellchecker相关推荐

  1. 【LeetCode】1641. Count Sorted Vowel Strings(动态规划)

    [LeetCode]1641. Count Sorted Vowel Strings(动态规划) Given an integer n, return the number of strings of ...

  2. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  3. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  4. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  5. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  6. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

  7. 【Leetcode】103. 二叉树的锯齿形层次遍历

    题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...

  8. 【Leetcode】79.单词搜索

    题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...

  9. 【leetcode】 算法题1 两数之和

    [leetcode] 算法题1 两数之和 问题   给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums ...

最新文章

  1. T-SQL基础(三)之子查询与表表达式
  2. 通知公告阅读日志构建说明
  3. SpringBoot-AOP切面处理
  4. kfc流程管理炸薯条几秒_炸薯条成为数据科学的最后前沿
  5. Maven高级之插件开发
  6. failed to open log file_C++中glog源码剖析以及如何设计一个高效 log模块
  7. 外链图片也有风险吗?
  8. 8盏流水灯反向闪烁c语言,课程设计(论文)_利用8255A芯片实现流水灯闪烁设计.doc...
  9. c语言主程序调用子程序数组,perl子程序返回多个数组到主程序中多个数组
  10. Picasso,Glide,Fresco对比分析
  11. oracle不能插入,oracle – 在过程中截断和插入不能一起工作
  12. 斐讯 K3刷梅林改固件ac-3100 成功配置定时任务
  13. C# 实现对三维点数据的 显示
  14. 盘点2022年大数据技术下常用的20个可视化工具,值得收藏
  15. 信息学奥赛一本通|1183:病人排队
  16. Fourier Neural Operator for Parametric Partial Differential Equations
  17. 表格固定表头,tbody加滚动条
  18. java简易计算器实验报告_Java 《Java 实现简单计算器》实验报告
  19. 【Python】批量修改照片文件名为拍摄日期
  20. AI时代的稀缺人才:全面剖析数据科学家成长的4个阶段

热门文章

  1. 网络资源下载方式:http/https、ftp/sftp、BT种子、磁力下载、ed2k下载等的区别
  2. Coreldraw绘制珍珠项链送给你
  3. 【报错笔记】Windows下spacy en_core_web_sm安装解决方式
  4. 阿里云 幸运券 分享 2017 10
  5. 看山是山,看水是水;看山不是山,看水不是水;看山还是山,看水还是水。
  6. 阿里巴巴达摩院发布2019十大科技趋势
  7. 怎样将网页保存为html,如何将网页保存为书签
  8. Linux/Centos: 服务器TIME_WAIT和CLOSE_WAIT区别及解决方案
  9. unity 控制移动的方法
  10. 定时器、Lambda表达式、Stream流