Vowel Spellchecker - Problem
๐ Build a Smart Spell Checker
Imagine building an auto-correct feature for a text editor! Given a dictionary of correctly spelled words (
Your spell checker should handle two types of errors with specific precedence rules:
๐ Capitalization Errors: If a query word matches a dictionary word ignoring case, return the original dictionary word with its proper capitalization.
Example: Dictionary has
๐ค Vowel Errors: If replacing any vowels (a,e,i,o,u) in the query makes it match a dictionary word (case-insensitive), return that dictionary word.
Example: Dictionary has
โ๏ธ Precedence Rules:
1. Exact match (case-sensitive) - highest priority
2. Case-insensitive match - return first match in wordlist
3. Vowel error match - return first match in wordlist
4. No match - return empty string
Given multiple queries, return the corrected word for each query following these rules.
Imagine building an auto-correct feature for a text editor! Given a dictionary of correctly spelled words (
wordlist), you need to implement a spell checker that fixes common typing mistakes in query words.Your spell checker should handle two types of errors with specific precedence rules:
๐ Capitalization Errors: If a query word matches a dictionary word ignoring case, return the original dictionary word with its proper capitalization.
Example: Dictionary has
"yellow", query is "YellOw" โ return "yellow"๐ค Vowel Errors: If replacing any vowels (a,e,i,o,u) in the query makes it match a dictionary word (case-insensitive), return that dictionary word.
Example: Dictionary has
"YellOw", query is "yollow" โ return "YellOw"โ๏ธ Precedence Rules:
1. Exact match (case-sensitive) - highest priority
2. Case-insensitive match - return first match in wordlist
3. Vowel error match - return first match in wordlist
4. No match - return empty string
Given multiple queries, return the corrected word for each query following these rules.
Input & Output
example_1.py โ Basic Spell Checking
$
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"]
๐ก Note:
- "kite" exact match โ "kite"<br>- "Kite" case-insensitive match with "kite" (first occurrence) โ "KiTe"<br>- "KiTe" exact match โ "KiTe"<br>- "Hare" exact match โ "Hare"<br>- "HARE" case-insensitive match with "hare" (first occurrence) โ "hare"<br>- "Hear" and "hear" no matches โ ""<br>- "keti" vowel pattern matches "KiTe" (k*t*) โ "KiTe"<br>- "keet" no match โ ""<br>- "keto" vowel pattern matches "KiTe" (k*t*) โ "KiTe"
example_2.py โ Precedence Rules
$
Input:
wordlist = ["yellow", "Yellow"]
queries = ["yellow", "Yellow", "YELLOW", "yollow"]
โบ
Output:
["yellow", "Yellow", "yellow", "yellow"]
๐ก Note:
- "yellow" exact match (highest priority) โ "yellow"<br>- "Yellow" exact match โ "Yellow"<br>- "YELLOW" case-insensitive match with "yellow" (first in wordlist) โ "yellow"<br>- "yollow" vowel pattern match with "yellow" (y*ll*w) โ "yellow"
example_3.py โ Edge Cases
$
Input:
wordlist = ["ae", "aa"]
queries = ["aei", "aea", "xyz"]
โบ
Output:
["", "ae", ""]
๐ก Note:
- "aei" has pattern "***" which doesn't match "ae" (*) or "aa" (**) โ ""<br>- "aea" has pattern "***" which doesn't match existing patterns โ ""<br>- "xyz" no matches in any category โ ""
Visualization
Tap to expand
Understanding the Visualization
1
Exact Match Check
First, check if the query exactly matches any word in our dictionary
2
Case-Insensitive Check
If no exact match, ignore capitalization and find the first matching word
3
Vowel Pattern Check
If still no match, replace vowels with wildcards and find pattern matches
4
No Match Found
If all checks fail, return empty string indicating no correction available
Key Takeaway
๐ฏ Key Insight: Build specialized hash maps for each matching type, then check them in precedence order for O(1) query processing!
Time & Space Complexity
Time Complexity
O(m ร n ร k)
m queries ร n words ร k average word length for string comparisons
โ Linear Growth
Space Complexity
O(1)
Only using variables for iteration, no extra data structures
โ Linear Space
Constraints
- 1 โค wordlist.length, queries.length โค 5000
- 1 โค wordlist[i].length, queries[i].length โค 7
- wordlist[i] and queries[i] consist only of English letters
- All words are case-sensitive for exact matching
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code