Vowel Spellchecker — LeetCode 966 Python Solution
- Problem
- #966
- Pattern
- Hash Map
- Reading time
- 5 min
- Source
- leetcode.com
The problem
Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word. For a given query word, the spell checker handles two categories of spelling mistakes: Capitalization: If the query matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the case in the wordlist.
Example
- Input
- wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
- Output
- ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
Python solution
class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
def f(w):
t = []
for c in w:
t.append("*" if c in "aeiou" else c)
return "".join(t)
s = set(wordlist)
low, pat = {}, {}
for w in wordlist:
t = w.lower()
low.setdefault(t, w)
pat.setdefault(f(t), w)
ans = []
for q in queries:
if q in s:
ans.append(q)
continue
q = q.lower()
if q in low:
ans.append(low[q])
continue
q = f(q)
if q in pat:
ans.append(pat[q])
continue
ans.append("")
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n + m) |
| Space | O(n), where n and m are the lengths of \textit{wordlist} and \textit{queries}, respectively auxiliary |
Pattern: Hash Map
Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 966. Vowel Spellchecker is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.
The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 966. Vowel Spellchecker?
- LeetCode 966. Vowel Spellchecker is rated Medium on LeetCode.
- What is the time complexity of LeetCode 966. Vowel Spellchecker?
- The Python solution on this page runs in O(n + m).
- What is the space complexity of LeetCode 966. Vowel Spellchecker?
- The Python solution on this page uses O(n), where n and m are the lengths of \textit{wordlist} and \textit{queries}, respectively auxiliary space.
- What topics does LeetCode 966. Vowel Spellchecker cover?
- LeetCode 966. Vowel Spellchecker is tagged Array, Hash Table and String on LeetCode.