2025-09-14 Daily Challenge
Today I have done leetcode's September LeetCoding Challenge with cpp
.
September LeetCoding Challenge 14
Description
Vowel Spellchecker
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.
<ul> <li>Example: <code>wordlist = ["yellow"]</code>, <code>query = "YellOw"</code>: <code>correct = "yellow"</code></li> <li>Example: <code>wordlist = ["Yellow"]</code>, <code>query = "yellow"</code>: <code>correct = "Yellow"</code></li> <li>Example: <code>wordlist = ["yellow"]</code>, <code>query = "yellow"</code>: <code>correct = "yellow"</code></li> </ul> </li> <li>Vowel Errors: If after replacing the vowels <code>('a', 'e', 'i', 'o', 'u')</code> of the query word with any vowel individually, it matches a word in the wordlist (<strong>case-insensitive</strong>), then the query word is returned with the same case as the match in the wordlist. <ul> <li>Example: <code>wordlist = ["YellOw"]</code>, <code>query = "yollow"</code>: <code>correct = "YellOw"</code></li> <li>Example: <code>wordlist = ["YellOw"]</code>, <code>query = "yeellow"</code>: <code>correct = ""</code> (no match)</li> <li>Example: <code>wordlist = ["YellOw"]</code>, <code>query = "yllw"</code>: <code>correct = ""</code> (no match)</li> </ul> </li>
In addition, the spell checker operates under the following precedence rules:
- When the query exactly matches a word in the wordlist (case-sensitive), you should return the same word back.
- When the query matches a word up to capitlization, you should return the first such match in the wordlist.
- When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
- If the query has no matches in the wordlist, you should return the empty string.
Given some queries
, return a list of words answer
, where answer[i]
is the correct word for query = queries[i]
.
Example 1:
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"]
Example 2:
Input: wordlist = ["yellow"], queries = ["YellOw"] Output: ["yellow"]
Constraints:
1 <= wordlist.length, queries.length <= 5000
1 <= wordlist[i].length, queries[i].length <= 7
wordlist[i]
andqueries[i]
consist only of only English letters.
Solution
class Solution {
string stringTolower(string word) {
for(auto &c : word) c = tolower(c);
return word;
}
string vowelUnify(string word) {
word = stringTolower(word);
for(auto &c : word) {
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
c = '?';
}
}
return word;
}
public:
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
unordered_set<string> words(wordlist.begin(), wordlist.end());
unordered_map<string, string> capitError;
unordered_map<string, string> vowelError;
for(auto word : wordlist) {
string vowel = vowelUnify(word);
if(!vowelError.count(vowel)) vowelError[vowel] = word;
string lower = stringTolower(word);
if(!capitError.count(lower)) capitError[lower] = word;
}
vector<string> answer;
for(auto query : queries) {
if(words.count(query)) {
answer.push_back(query);
continue;
}
string lower = stringTolower(query);
if(capitError.count(lower)) {
answer.push_back(capitError[lower]);
continue;
}
string vowel = vowelUnify(query);
if(vowelError.count(vowel)) {
answer.push_back(vowelError[vowel]);
continue;
}
answer.push_back("");
}
return answer;
}
};
// Accepted
// 55/55 cases passed (42 ms)
// Your runtime beats 49.37 % of cpp submissions
// Your memory usage beats 30.38 % of cpp submissions (42.7 MB)