-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0966-vowel-spellchecker.py
More file actions
45 lines (38 loc) · 1.34 KB
/
0966-vowel-spellchecker.py
File metadata and controls
45 lines (38 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# time complexity: O(n)
# space complexity: O(n)
from typing import List
class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
exact = set(wordlist)
caseMap = {}
vowelMap = {}
for word in wordlist:
lower = word.lower()
devowel = self.deVowel(lower)
if lower not in caseMap:
caseMap[lower] = word
if devowel not in vowelMap:
vowelMap[devowel] = word
result = []
for query in queries:
if query in exact:
result.append(query)
else:
lower = query.lower()
devowel = self.deVowel(lower)
if lower in caseMap:
result.append(caseMap[lower])
elif devowel in vowelMap:
result.append(vowelMap[devowel])
else:
result.append("")
return result
def deVowel(self, s):
return ''.join('*' if c in 'aeiou' else c for c in s)
wordlist = ["KiTe", "kite", "hare", "Hare"]
queries = ["kite", "Kite", "KiTe", "Hare", "HARE",
"Hear", "hear", "keti", "keet", "keto"]
print(Solution().spellchecker(wordlist, queries))
wordlist = ["yellow"]
queries = ["YellOw"]
print(Solution().spellchecker(wordlist, queries))