1 class Solution(object): 2 def spellchecker(self, wordlist: 'List[str]', queries: 'List[str]') -> 'List[str]': 3 wordlen = len(wordlist) 4 wordict = {}#存原始形式 5 wordict_lowercase = {}#存小写形式 6 wordict_replace = {}#存替换形式 7 for i in range(wordlen): 8 word = wordlist[i] 9 if word not in wordict: 10 wordict.update({word:i}) 11 12 lowcase = wordlist[i].lower() 13 #if lowcase not in wordict_lowercase and lowcase != word: 14 if lowcase not in wordict_lowercase: 15 wordict_lowercase.update({lowcase:i}) 16 17 altercase = '' 18 for j in range(len(lowcase)): 19 c = lowcase[j] 20 if c=='e' or c =='i' or c =='o' or c=='u': 21 c ='a' 22 altercase += c 23 if altercase not in wordict_replace: 24 wordict_replace.update({altercase:i}) 25 ''' 26 print(wordict) 27 print(wordict_lowercase) 28 print(wordict_replace) 29 ''' 30 result = list() 31 for i in range(len(queries)): 32 q = queries[i] 33 lowq = q.lower() 34 35 alterq = '' 36 for j in range(len(lowq)): 37 c = lowq[j] 38 if c == 'e' or c == 'i' or c == 'o' or c =='u': 39 c = 'a' 40 alterq += c 41 if q in wordict: 42 result.append(wordlist[wordict[q]]) 43 elif lowq in wordict_lowercase: 44 result.append(wordlist[wordict_lowercase[lowq]]) 45 elif alterq in wordict_replace: 46 result.append(wordlist[wordict_replace[alterq]]) 47 else: 48 result.append('') 49 return result