[
hash table
string
]
Leetcode 0966. Vowel Spellchecker
https://leetcode.com/problems/vowel-spellchecker
Not very difficult problem, you just need to be careful and use idea of masks here. For example for word heLLO
I define mask like this h*ll*
, that is put all letters to lower case and replace all vowels to *
. Why it is good idea to do it? Because if two words have the same mask, it means, that one of them is correction of another. We need to check 3
cases:
- If
query
word in originalwordlist
, we just return this word. - If
query.lower()
word ind1
: dictionary for words without corrections, which is correspondence between word and its lower case, for exampleheLlO: hello
. If we foundquery
in this dictionary, by problem statement we need to return the first such match in the wordlist: that is why when we defined1
, we from the end and rewrite matches if we have more than one. - Finally, if
mask(query)
in dictionaryd2
, we returnd2[mask(query)]
. Note, that we definedd2
also from the end.
Complexity: it is O(M + N)
, where M
is total length of words in wordlist
and M
is total length of words in queries
, because we process each word from wordlist
no more than 3
times and each word from query 1
time.
class Solution:
def spellchecker(self, wordlist, queries):
def mask(w):
return "".join('*' if c in 'aeiou' else c for c in w.lower())
d0 = set(wordlist)
d1 = {w.lower(): w for w in wordlist[::-1]}
d2 = {mask(w): w for w in wordlist[::-1]}
def solve(query):
if query in d0: return query
if query.lower() in d1: return d1[query.lower()]
if mask(query) in d2: return d2[mask(query)]
return ""
return [solve(q) for q in queries]
If you like the solution, you can upvote it on leetcode discussion section: Problem 0966