Problem Description
Given a wordlist and a set of queries, implement a spellchecker that corrects a query word by considering three levels of matching:
- Exact match (case-sensitive).
- Case-insensitive match.
- Match after replacing all vowels (a, e, i, o, u) in the query with a placeholder (treating any vowel as equivalent). Return the first word from the wordlist that matches the query based on the highest-precedence rule. If no match exists, return an empty string.
Key Insights
- Use hash tables to efficiently check for exact, case-insensitive, and vowel-error matches.
- For case-insensitive matching, store mapping of lowercase version of each word to the original word (only storing the first occurrence).
- For vowel error matching, transform each word by replacing all vowels with a common character (e.g., '#') and map the transformed word to the original word (again, only the first occurrence).
- First check for an exact match. If not, check the case-insensitive dictionary. Then, check the vowel-error dictionary.
- Use helper functions to convert words to their vowel-normalized form.
Space and Time Complexity
Time Complexity: O(N * L + Q * L) where N is the number of words in the wordlist, Q is the number of queries, and L is the maximum length of words. This is because we process each word letter by letter. Space Complexity: O(N * L) due to auxiliary dictionaries storing modified forms of all words.
Solution
We build three dictionaries:
- An exact set of words to enable O(1) checks for matches.
- A case-insensitive dictionary mapping the lowercase word to the first occurrence in the wordlist.
- A vowel-error dictionary mapping a transformed word (where vowels are replaced with a placeholder, such as '#') to the first word from the wordlist.
For each query, we:
- Check if the query exists exactly in the wordlist.
- Check if the lowercase form of the query exists in the case-insensitive dictionary.
- Check if the transformed query (using the same vowel removal pattern) exists in the vowel-error dictionary. If none of the above yield a match, return an empty string.