作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/vowel-spellchecker/description/

题目描述

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.

题目大意

现在给了一个单词字典,给出了一堆要查询的词,要返回查询结果。查询的功能如下:

  1. 如果字典里有现在的单词,就直接返回;
  2. 如果不满足1,那么判断能不能更改要查询单词的某些大小写使得结果在字典中,如果字典里多个满足条件的,就返回第一个;
  3. 如果不满足2,那么判断能不能替换要查询单词的元音字符成其他的字符使得结果在字典中,如果字典里多个满足条件的,就返回第一个;
  4. 如果不满足4,返回查询的结果是空字符串。

解题方法

字典

这个题还是挺烦的,并不是一个考察搜索的题目,可以直接使用字典去解决。

首先,判断有没有相同的单词,这个很好办,直接使用set;
其次,要判断改变某些大小写,这里注意的是可以把要查询的字符串中的任意字符转换成大小写,如果抽象一点的话就是,忽略字符串所有字符的大小写之后匹配即可。因为要返回第一个出现的,所以,我们把要字典字符串反过来构成字典,这样就保存了忽略大小写之后的字符串第一个出现的位置。
最后,要把元音字符进行忽略,可以任意转换。这个思路很邪,直接把元音字符转成同样的字符,这样只要把元音统一替换之后的结果相等即可。同样反向构成字典。

python代码如下:

class Solution(object):
def spellchecker(self, wordlist, queries):
"""
:type wordlist: List[str]
:type queries: List[str]
:rtype: List[str]
"""
wordset = set(wordlist)
capdict = {word.lower() : word for word in wordlist[::-1]}
vodict = {re.sub(r'[aeiou]', '#', word.lower()) : word for word in wordlist[::-1]}
res = []
for q in queries:
if q in wordset:
res.append(q)
elif q.lower() in capdict:
res.append(capdict[q.lower()])
elif re.sub(r'[aeiou]', '#', q.lower()) in vodict:
res.append(vodict[re.sub(r'[aeiou]', '#', q.lower())])
else:
res.append("")
return res

日期

2018 年 12 月 30 日 —— 周赛差强人意

【LeetCode】966. Vowel Spellchecker 解题报告(Python)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. windows下的python安装pysam报错

    安装pysam时报错: 指定版本仍报错: 使用pysam-win安装: 但是import时不行: 貌似pysam在windows下难以正常配置,还是在Linux中用吧. https://www.jia ...

  2. Linux之文件读取查看之cat、head、tail、tac、rev、more、less

    Linux文件查看的命令有很多,如cat.head.tail.tac.rev.more.less等 1. cat之查看文件内容 NAME cat - 连接文件并在标准输出上打印(concatenate ...

  3. STM32 部分重映射和完全重映射(查看数据手册)

    数据手册如何查找对应的映射: 打开官网直接搜索STM32F可以看到数据手册,里面有关于重映射的表格,输入第6页的页码,点击9.3中的9.3x可打开对应的链接.  举例说明: STM32中拥有重映射功能 ...

  4. spring boot-jpa整合QueryDSL来简化复杂操作

    spring boot-jpa整合QueryDSL来简化复杂操作 SpringDataJPA+QueryDSL玩转态动条件/投影查询  

  5. swift设置导航栏item颜色和状态栏颜色

    //swift设置导航栏item颜色和状态栏颜色 let dict:Dictionary =[NSForegroundColorAttributeName:UIColor.hrgb("333 ...

  6. java对象分配

    1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代完全可以,分代的唯一理由就是优化GC性能.你先想想,如果没有分代,那我们所有的对象都在一块,GC的时候我 ...

  7. shell脚本计算Linux网卡流量

    本文介绍了计算linux网卡流量的一个shell脚本,一个通过固定间隔时间获取ifconfig eth0 的字节值而计算出网卡流量的方法,有需要的朋友参考下. 使用shell脚本计算Linux网卡流量 ...

  8. 二叉搜索树、平衡二叉树、红黑树、B树、B+树

    完全二叉树: 空树不是完全二叉树,叶子结点只能出现在最下层和次下层,且最下层的叶子结点集中在树的左部.如果遇到一个结点,左孩子不为空,右孩子为空:或者左右孩子都为空:则该节点之后的队列中的结点都为叶子 ...

  9. Markdown随时记录

    Markdown学习 推荐文本编译器 Typora 标题(支持六级) 一级标题:# + 空格 + 内容 二级标题:## + 空格 + 内容 三级标题:### + 空格 + 内容 . . . 字体 粗体 ...

  10. idea 无法创建子目录

    idea 无法创建子目录 解决方案