博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】966. Vowel Spellchecker
阅读量:6223 次
发布时间:2019-06-21

本文共 3430 字,大约阅读时间需要 11 分钟。

题目如下:

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

 

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

你可能感兴趣的文章
1.7. User interfaces
查看>>
阿里Druid数据连接池在SSM框架中的配置使用
查看>>
基于Metronic的Bootstrap开发框架经验总结(17)-- 使用 summernote插件实现HTML文档的编辑和图片插入操作...
查看>>
Linux虚拟主机通过程序实现二级域名绑定到子目录
查看>>
7.12. cvs diff
查看>>
Android酷炫实用的开源框架(UI框架)
查看>>
Winform开发框架之对话框样式同化
查看>>
一脸懵逼学习Linux的Shell编程
查看>>
Jmeter调试工具---Debug Sampler
查看>>
[裴礼文数学分析中的典型问题与方法习题参考解答]4.5.14
查看>>
impdp的TABLE_EXISTS_ACTION参数选项
查看>>
机器学习之深入理解神经网络理论基础、BP算法及其Python实现
查看>>
ecshop设置一个子类对应多个父类并指定跳转url的修改方法
查看>>
【spring源码学习】spring的事务管理的源码解析
查看>>
遇见喜欢数学的女孩
查看>>
linux进程资源占用高原因分析命令记录
查看>>
【转】solr+ajax智能拼音详解---solr跨域请求
查看>>
SOA架构设计经验分享—架构、职责、数据一致性
查看>>
微信开发之推广支持
查看>>
第 50 章 Resin
查看>>