Home > Article > Backend Development > Count the Number of Consistent Strings
1684. Count the Number of Consistent Strings
Difficulty: Easy
Topics: Array, Hash Table, String, Bit Manipulation, Counting
You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed.
Return the number of consistent strings in the array words.
Example 1:
Example 2:
Example 3:
Constraints:
Hint:
Solution:
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:
Word Consistency Check:
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) { ... ... ... /** * go to ./solution.php */ } // 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 ?> <h3> Explanation: </h3> <ol> <li> <p><strong>Allowed Set</strong>:</p> <ul> <li>We create an associative array $allowedSet where each key is a character from the allowed string. This allows for fast lookups.</li> </ul> </li> <li> <p><strong>Word Consistency</strong>:</p> <ul> <li>For each word in the 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.</li> </ul> </li> <li> <p><strong>Counting</strong>:</p> <ul> <li>Every time we find a consistent word, we increment the counter $consistentCount.</li> </ul> </li> <li> <p><strong>Return the Result</strong>:</p> <ul> <li>After processing all words, the counter holds the number of consistent strings, which we return.</li> </ul> </li> </ol> <h3> Time Complexity: </h3> <ul> <li> <strong>Time Complexity</strong>: O(n * m), where 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.</li> </ul> <h3> Example Walkthrough: </h3> <p>For the input:<br> </p> <pre class="brush:php;toolbar:false">$allowed = "ab"; $words = ["ad", "bd", "aaab", "baa", "badab"];
Thus, the function returns 2.
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:
The above is the detailed content of Count the Number of Consistent Strings. For more information, please follow other related articles on the PHP Chinese website!