Closed mah-shamim closed 5 days ago
The idea is to check if each word in the words
array is consistent with the characters in the allowed
string. A word is consistent if all its characters are present in the allowed
string.
Allowed Characters Set:
allowed
string into a set of characters to efficiently check if each character in the word exists in the set.Word Consistency Check:
words
array, check if all its characters exist in the allowed
set.Count Consistent Words:
Return the Count:
Let's implement this solution in PHP: 1684. Count the Number of Consistent Strings
<?php
/**
* @param String $allowed
* @param String[] $words
* @return Integer
*/
function countConsistentStrings($allowed, $words) {
// Step 1: Create a set (array) for allowed characters
$allowedSet = [];
for ($i = 0; $i < strlen($allowed); $i++) {
$allowedSet[$allowed[$i]] = true;
}
// Step 2: Initialize counter for consistent strings
$consistentCount = 0;
// Step 3: Check each word in words array
foreach ($words as $word) {
$isConsistent = true;
for ($j = 0; $j < strlen($word); $j++) {
// If the character is not in the allowed set, mark the word as inconsistent
if (!isset($allowedSet[$word[$j]])) {
$isConsistent = false;
break;
}
}
// Step 4: If the word is consistent, increment the counter
if ($isConsistent) {
$consistentCount++;
}
}
// Step 5: Return the count of consistent strings
return $consistentCount;
}
// Example usage:
// Example 1:
$allowed = "ab";
$words = ["ad", "bd", "aaab", "baa", "badab"];
echo countConsistentStrings($allowed, $words); // Output: 2
// Example 2:
$allowed = "abc";
$words = ["a","b","c","ab","ac","bc","abc"];
echo countConsistentStrings($allowed, $words); // Output: 7
// Example 3:
$allowed = "cad";
$words = ["cc","acd","b","ba","bac","bad","ac","d"];
echo countConsistentStrings($allowed, $words); // Output: 4
?>
Allowed Set:
$allowedSet
where each key is a character from the allowed
string. This allows for fast lookups.Word Consistency:
words
array, we loop through its characters and check if they are in $allowedSet
. If we find any character that isn't in the set, the word is marked as inconsistent, and we move on to the next word.Counting:
$consistentCount
.Return the Result:
n
is the number of words and m
is the average length of the words. We are iterating through all the words and their characters.For the input:
$allowed = "ab";
$words = ["ad", "bd", "aaab", "baa", "badab"];
allowedSet = ['a' => true, 'b' => true]
.Thus, the function returns 2
.
allowed
can only have up to 26 distinct characters, and words
has at most 10,000 entries, this brute-force solution is efficient enough given the constraints. Each word can have a maximum length of 10, making it feasible to iterate over all characters.
Discussed in https://github.com/mah-shamim/leet-code-in-php/discussions/530
1 <= words.length <= 104
-1 <= allowed.length <= 26
-1 <= words[i].length <= 10
- The characters in `allowed` are **distinct**. - `words[i]` and `allowed` contain only lowercase English letters. **Hint:** 1. A string is incorrect if it contains a character that is not allowed 2. Constraints are small enough for brute force