Challenge: Sarcasm Treatment of an RSS Feed Item (966. Vowel Spellchecker)
The task involves a RSS feed item that contains a list of words, including Array
, Hash Table
, String
, and a weekly contest. The goal is to implement a spell checker that converts a query word into the correct word, taking into account the following rules:1. Capitalization: If the query matches a word in the wordlist, return the same case as the case in the wordlist.
2. Vowel Errors: If after replacing the vowels with a placeholder ('*'), the query matches a word in the wordlist, return the first such match in the wordlist.
3. No Match: If none of the above checks succeed, return an empty string.
The provided RSS feed item contains the following text:
“`yaml
966. Vowel Spellchecker
Difficulty: Medium
Topics: 966. Vowel Spellchecker Difficulty: Medium Topics: Given a For a given In addition, the spell checker operates under the following precedence rules: Given some Example 1: Example 2: Constraints: Solution: We need to implement a spell checker that handles two types of spelling mistakes: capitalization errors and vowel errors. The solution involves checking the query word against the wordlist in a specific order of precedence: exact match, case-insensitive match, and then vowel-insensitive match. Approach Let's implement this solution in PHP: 966. Vowel Spellchecker
Explanation:Array
, Hash Table
, String
, Weekly Contest 117
Array
, Hash Table
, String
, Weekly Contest 117
wordlist
, we want to implement a spellchecker that converts a query word into a correct word.query
word, the spell checker handles two categories of spelling mistakes:
wordlist = ["yellow"]
, query = "YellOw"
: correct = "yellow"
wordlist = ["Yellow"]
, query = "yellow"
: correct = "Yellow"
wordlist = ["yellow"]
, query = "yellow"
: correct = "yellow"
'a'
, 'e'
, 'i'
, 'o'
, 'u'
) of the query word with any vowel individually, it matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the match in the wordlist.
wordlist = ["YellOw"]
, query = "yollow"
: correct = "YellOw"
wordlist = ["YellOw"]
, query = "yeellow"
: correct = ""
(no match)wordlist = ["YellOw"]
, query = "yllw":
correct = ""
(no match)
queries
, return a list of words answer
, where answer[i]
is the correct word for query = queries[i]
.
1 <= wordlist.length, queries.length <= 5000
1 <= wordlist[i].length, queries[i].length <= 7
wordlist[i]
and queries[i]
consist only of only English letters.
<?php
/**
* @param String[] $wordlist
* @param String[] $queries
* @return String[]
*/
function spellchecker($wordlist, $queries) {
...
...
...
/**
* go to ./solution.php
*/
}
/**
* @param $s
* @return array|string|string[]
*/
function devowel($s) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
$wordlist = ["KiTe","kite","hare","Hare"];
$queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"];
print_r(spellchecker($wordlist, $queries));
// Expected: ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
$wordlist2 = ["yellow"];
$queries2 = ["YellOw"];
print_r(spellchecker($wordlist2, $queries2));
// Expected: ["yellow"]
?>
-
$exactMap
to store exact words from the wordlist. -
$caseMap
to store the first occurrence of each case-insensitive version of words. -
$vowelMap
to store the first occurrence of each vowel-insensitive version of words.
- Add the word to
$exactMap
. - Convert the word to lowercase and store it in
$caseMap
if not already present. - Convert the word to a vowel-insensitive form (lowercase with vowels replaced by '*') and store it in
$vowelMap
if not already present.
- Check for an exact match in
$exactMap
. - If not found, check for a case-insensitive match in
$caseMap
. - If still not found, check for a vowel-insensitive match in
$vowelMap
. - If no match is found, return an empty string.
devowel
function replaces all vowels in a string with '*', used for both processing the wordlist and queries.This approach efficiently checks for matches in the order of precedence specified, ensuring correct handling of capitalization and vowel errors while leveraging hash maps for optimal performance.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me: