966. Vowel Spellchecker #2172
-
Topics: Given a For a given
In addition, the spell checker operates under the following precedence rules:
Given some Example 1:
Example 2:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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 <?php
/**
* @param String[] $wordlist
* @param String[] $queries
* @return String[]
*/
function spellchecker($wordlist, $queries) {
$exactMap = [];
$caseMap = [];
$vowelMap = [];
foreach ($wordlist as $word) {
$exactMap[$word] = true;
$lower = strtolower($word);
if (!isset($caseMap[$lower])) {
$caseMap[$lower] = $word;
}
$devoweled = devowel($lower);
if (!isset($vowelMap[$devoweled])) {
$vowelMap[$devoweled] = $word;
}
}
$result = [];
foreach ($queries as $query) {
if (isset($exactMap[$query])) {
$result[] = $query;
continue;
}
$lowerQuery = strtolower($query);
if (isset($caseMap[$lowerQuery])) {
$result[] = $caseMap[$lowerQuery];
continue;
}
$devoweledQuery =devowel($lowerQuery);
if (isset($vowelMap[$devoweledQuery])) {
$result[] = $vowelMap[$devoweledQuery];
continue;
}
$result[] = "";
}
return $result;
}
/**
* @param $s
* @return array|string|string[]
*/
function devowel($s) {
return str_replace(['a', 'e', 'i', 'o', 'u'], '*', $s);
}
// 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"]
?> Explanation:
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. |
Beta Was this translation helpful? Give feedback.
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