-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path966.py
64 lines (51 loc) · 2.06 KB
/
966.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
__________________________________________________________________________________________________
sample 192 ms submission
class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
def removevowels(word):
for x in ['a', 'e', 'i', 'o', 'u']:
word = word.replace(x, '.')
return word
X, Y = {}, {}
for word in wordlist:
x = word.lower()
if x in X:
X[x] += [word]
else:
X[x] = [word]
y = removevowels(x)
if y not in Y:
Y[y] = word
for i, q in enumerate(queries):
x = q.lower()
if x in X:
queries[i] = q if q in X[x] else X[x][0]
else:
y = removevowels(x)
queries[i] = Y[y] if y in Y else ''
return queries
__________________________________________________________________________________________________
sample 196 ms submission
class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
def devowel(words):
return "".join("*" if letter in 'aeiou' else letter for letter in words)
wordOrig = set(wordlist)
wordCap = {}
wordVow = {}
for word in wordlist:
wordLow = word.lower()
wordCap.setdefault(wordLow, word)
wordVow.setdefault(devowel(wordLow), word)
def helper(query):
if query in wordOrig:
return query
queryLow = query.lower()
if queryLow in wordCap:
return wordCap[queryLow]
queryVow = devowel(queryLow)
if queryVow in wordVow:
return wordVow[queryVow]
return ''
return map(helper, queries)
__________________________________________________________________________________________________