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 (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
Spell Checker Decision FlowQuery: "YellOw"1. Exact Match"YellOw" in wordlist?โœ— No2. Case-Insensitive"yellow" in case_map?โœ“ Yes โ†’ "yellow"3. Vowel Pattern"y*ll*w" in vowel_map?Skipped4. No MatchReturn ""Not reachedResult: "yellow"Hash Table Preprocessing:exact_set = {"yellow", "Yellow"}case_map = {"yellow": "yellow"}vowel_map = {"y*ll*w": "yellow"}
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using variables for iteration, no extra data structures

n
2n
โœ“ 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
Asked in
Google 42 Microsoft 28 Amazon 31 Facebook 19
18.4K Views
Medium Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen