|
| 1 | +''' |
| 2 | + You are given a string allowed consisting of distinct |
| 3 | + characters and an array of strings words. A string is |
| 4 | + consistent if all characters in the string appear in the |
| 5 | + string allowed. |
| 6 | +
|
| 7 | + Return the number of consistent strings in the array words. |
| 8 | +
|
| 9 | + Example: |
| 10 | + Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"] |
| 11 | + Output: 2 |
| 12 | + Explanation: Strings "aaab" and "baa" are consistent since |
| 13 | + they only contain characters 'a' and 'b'. |
| 14 | +
|
| 15 | + Example: |
| 16 | + Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"] |
| 17 | + Output: 7 |
| 18 | + Explanation: All strings are consistent. |
| 19 | +
|
| 20 | + Example: |
| 21 | + Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"] |
| 22 | + Output: 4 |
| 23 | + Explanation: Strings "cc", "acd", "ac", and "d" are consistent. |
| 24 | +
|
| 25 | + Constraints: |
| 26 | + - 1 <= words.length <= 10^4 |
| 27 | + - 1 <= allowed.length <= 26 |
| 28 | + - 1 <= words[i].length <= 10 |
| 29 | + - The characters in allowed are distinct. |
| 30 | + - words[i] and allowed contain only lowercase |
| 31 | + English letters. |
| 32 | +''' |
| 33 | +#Difficulty: Easy |
| 34 | +#74 / 74 test cases passed. |
| 35 | +#Runtime: 248 ms |
| 36 | +#Memory Usage: 16.3 MB |
| 37 | + |
| 38 | +#Runtime: 248 ms, faster than 75.00% of Python3 online submissions for Count the Number of Consistent Strings. |
| 39 | +#Memory Usage: 16.3 MB, less than 25.00% of Python3 online submissions for Count the Number of Consistent Strings. |
| 40 | + |
| 41 | +class Solution: |
| 42 | + def countConsistentStrings(self, allowed: str, words: List[str]) -> int: |
| 43 | + count = len(words) |
| 44 | + for word in words: |
| 45 | + word = set(word) |
| 46 | + for char in word: |
| 47 | + if char not in allowed: |
| 48 | + count -= 1 |
| 49 | + break |
| 50 | + return count |
0 commit comments