-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvowel-spellchecker.py
64 lines (58 loc) · 1.57 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
aeiou = {
'a':True,
'e':True,
'i':True,
'o':True,
'u':True,
}
def perm(listword):
ans = []
if listword[0] in aeiou:
if len(listword) == 1:
ans += ['a','e','i','o','u']
else:
perms = perm(listword[1:])
for l in ['a','e','i','o','u']:
for p in perms:
ans.append(l + p)
else:
if len(listword) == 1:
ans.append(listword[0])
else:
perms = perm(listword[1:])
for p in perms:
ans.append(listword[0] + p)
return ans
class Solution(object):
def spellchecker(self, wordlist, queries):
"""
:type wordlist: List[str]
:type queries: List[str]
:rtype: List[str]
"""
exact = {}
capital = {}
vowel = {}
for w in wordlist:
exact[w] = w
lower = w.lower()
if lower not in capital:
capital[lower] = w
perms = perm(list(lower))
for p in perms:
if p not in vowel:
vowel[p] = w
ans = []
for q in queries:
if q in exact:
ans.append(q)
continue
lower = q.lower()
if lower in capital:
ans.append(capital[lower])
continue
if lower in vowel:
ans.append(vowel[lower])
continue
ans.append('')
return ans