Leetcode 966: Vowel Spellchecker

grid47
grid47
Exploring patterns and algorithms
Aug 2, 2024 7 min read

Design a spellchecker that corrects a given word from a query based on specific rules. For each query, find a matching word from the given wordlist by applying exact matches, case-insensitive matches, or vowel error corrections. Return the corrected word, or an empty string if no match is found.
Problem
Approach
Steps
Complexity
Input: The problem takes in a wordlist of valid words and a set of query words to check against the wordlist.
Example: "wordlist": ["Orange", "blue", "Red", "green"], "queries": ["orange", "BLUE", "rEd", "grane", "yellow"]
Constraints:
• 1 <= wordlist.length, queries.length <= 5000
• 1 <= wordlist[i].length, queries[i].length <= 7
• Each word in wordlist and queries consists only of English letters.
Output: Return a list of corrected words for the queries based on the rules, or an empty string for unmatched queries.
Example: ["Orange", "blue", "Red", "", ""]
Constraints:
• The output list must have the same length as the queries.
• Each output corresponds to the corrected word for the respective query.
Goal: Find the best match for each query using exact matching, case-insensitive matching, or vowel-error correction.
Steps:
• Convert the wordlist into sets and maps for fast lookups.
• Check if the query matches a word exactly in the wordlist.
• If no exact match, check for case-insensitive matches.
• If no case-insensitive match, replace vowels in the query and check for vowel-error matches.
• Return the first match for each query based on precedence or an empty string if no match is found.
Goal: Limitations for input size and rules for query matching.
Steps:
• Each query is matched based on strict precedence: exact match > case-insensitive match > vowel-error match.
• Words in the wordlist are unique.
Assumptions:
• All queries will be processed independently.
• The wordlist and queries contain only valid English words.
Input: "wordlist": ["Orange", "blue", "Red", "green"], "queries": ["orange", "BLUE", "rEd", "grane", "yellow"]
Explanation: Query 'orange' matches 'Orange' exactly ignoring case, 'BLUE' matches 'blue', 'rEd' matches 'Red'. Query 'grane' has a vowel error but doesn't match any word. 'yellow' has no match in the wordlist. Output: ['Orange', 'blue', 'Red', '', ''].

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus