1. 程式人生 > >【leetcode】966. Vowel Spellchecker

【leetcode】966. Vowel Spellchecker

題目如下:

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