题目如下:

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

最新文章

  1. 关于SQL语句查询区分大小写
  2. CSS-float详解,深入理解clear:both[转+部分原创]
  3. 进监狱全攻略之 Mifare1 Card 破解
  4. phpcms v9调用多个栏目下文章的方法
  5. addListener添加事件监听器,第三个参数useCapture (Boolean) 的作用
  6. MongoDB性能优化指南
  7. 创建渐进式jpeg图片
  8. VMware vSphere服务器虚拟化实验十五 vCenter vShield Manager
  9. 让.NET程序快速释放内存的办法
  10. python︱函数、for、_name_杂记
  11. Compass 更智能的搜索引擎(3)--高亮,排序,过滤以及各种搜索
  12. HDU - 1035
  13. 切割日志(mysql,nginx,php tomcat)使用logrotate
  14. setTimeout运行机制简要理解
  15. LVM基础详细说明及动态扩容lvm逻辑卷的操作记录
  16. intent-filter 之 data 「scheme, host, port, mimeType, path, pathPrefix, pathPattern」
  17. html5-css的使用强制优先级
  18. WAS 常见报错
  19. thinkphp5的Redis缓存配置
  20. Python之旅:流程控制

热门文章

  1. ivew-admin 点击预览图片
  2. Linux的软件包管理
  3. 二叉树入门(洛谷P1305)
  4. 区间第k大的几种解法
  5. layui导出表格全部数据
  6. paper 143:人脸验证
  7. AcWing 220.最大公约数 欧拉函数打卡
  8. python re.findall(rule,data),根据左右边界取值url中参数的值
  9. 转-C++之string判断字符串是否包含某个子串
  10. Jmeter 5.1参数化csv引入文件