forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vowel-spellchecker.py
34 lines (30 loc) · 935 Bytes
/
vowel-spellchecker.py
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
# Time: O(n)
# Space: O(w)
class Solution(object):
def spellchecker(self, wordlist, queries):
"""
:type wordlist: List[str]
:type queries: List[str]
:rtype: List[str]
"""
vowels = set(['a', 'e', 'i', 'o', 'u'])
def todev(word):
return "".join('*' if c.lower() in vowels else c.lower()
for c in word)
words = set(wordlist)
caps = {}
vows = {}
for word in wordlist:
caps.setdefault(word.lower(), word)
vows.setdefault(todev(word), word)
def check(query):
if query in words:
return query
lower = query.lower()
if lower in caps:
return caps[lower]
devow = todev(lower)
if devow in vows:
return vows[devow]
return ""
return map(check, queries)