|
| 1 | +### 题目描述 |
| 2 | + |
| 3 | +这是 LeetCode 上的 **[1684. 统计一致字符串的数目](https://leetcode.cn/problems/count-the-number-of-consistent-strings/solution/by-ac_oier-j2kj/)** ,难度为 **简单**。 |
| 4 | + |
| 5 | +Tag : 「模拟」、「位运算」 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +给你一个由不同字符组成的字符串 `allowed` 和一个字符串数组 `words`。如果一个字符串的每一个字符都在 `allowed` 中,就称这个字符串是 一致字符串 。 |
| 10 | + |
| 11 | +请你返回 `words` 数组中 一致字符串 的数目。 |
| 12 | + |
| 13 | +示例 1: |
| 14 | +``` |
| 15 | +输入:allowed = "ab", words = ["ad","bd","aaab","baa","badab"] |
| 16 | +
|
| 17 | +输出:2 |
| 18 | +
|
| 19 | +解释:字符串 "aaab" 和 "baa" 都是一致字符串,因为它们只包含字符 'a' 和 'b' 。 |
| 20 | +``` |
| 21 | +示例 2: |
| 22 | +``` |
| 23 | +输入:allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"] |
| 24 | +
|
| 25 | +输出:7 |
| 26 | +
|
| 27 | +解释:所有字符串都是一致的。 |
| 28 | +``` |
| 29 | +示例 3: |
| 30 | +``` |
| 31 | +输入:allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"] |
| 32 | +
|
| 33 | +输出:4 |
| 34 | +
|
| 35 | +解释:字符串 "cc","acd","ac" 和 "d" 是一致字符串。 |
| 36 | +``` |
| 37 | + |
| 38 | +提示: |
| 39 | +* $1 <= words.length <= 10^4$ |
| 40 | +* $1 <= allowed.length <= 26$ |
| 41 | +* $1 <= words[i].length <= 10$ |
| 42 | +* `allowed` 中的字符 互不相同 。 |
| 43 | +* `words[i]` 和 `allowed` 只包含小写英文字母。 |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +### 模拟 |
| 48 | + |
| 49 | +根据题意模拟即可:为了快速判断某个字符是否在 `allowed` 出现过,我们可以使用 `Set` 结构、定长数组或是一个 `int` 变量(搭配位运算)来对 `allowed` 中出现的字符进行转存。 |
| 50 | + |
| 51 | +随后遍历所有的 $words[i]$,统计符合要求的字符串个数。 |
| 52 | + |
| 53 | +Java 代码: |
| 54 | +```Java |
| 55 | +class Solution { |
| 56 | + public int countConsistentStrings(String allowed, String[] words) { |
| 57 | + boolean[] hash = new boolean[26]; |
| 58 | + for (char c : allowed.toCharArray()) hash[c - 'a'] = true; |
| 59 | + int ans = 0; |
| 60 | + out:for (String s : words) { |
| 61 | + for (char c : s.toCharArray()) { |
| 62 | + if (!hash[c - 'a']) continue out; |
| 63 | + } |
| 64 | + ans++; |
| 65 | + } |
| 66 | + return ans; |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | +Java 代码: |
| 71 | +```Java |
| 72 | +class Solution { |
| 73 | + public int countConsistentStrings(String allowed, String[] words) { |
| 74 | + int hash = 0, ans = 0; |
| 75 | + for (char c : allowed.toCharArray()) hash |= (1 << (c - 'a')); |
| 76 | + out:for (String s : words) { |
| 77 | + for (char c : s.toCharArray()) { |
| 78 | + if (((hash >> (c - 'a')) & 1) == 0) continue out; |
| 79 | + } |
| 80 | + ans++; |
| 81 | + } |
| 82 | + return ans; |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | +TypeScript 代码: |
| 87 | +```TypeScript |
| 88 | +function countConsistentStrings(allowed: string, words: string[]): number { |
| 89 | + const sset = new Set<string>() |
| 90 | + for (const c of allowed) sset.add(c) |
| 91 | + let ans = 0 |
| 92 | + out:for (const s of words) { |
| 93 | + for (const c of s) { |
| 94 | + if (!sset.has(c)) continue out |
| 95 | + } |
| 96 | + ans++ |
| 97 | + } |
| 98 | + return ans |
| 99 | +} |
| 100 | +``` |
| 101 | +TypeScript 代码: |
| 102 | +```TypeScript |
| 103 | +function countConsistentStrings(allowed: string, words: string[]): number { |
| 104 | + let hash = 0, ans = 0 |
| 105 | + for (const c of allowed) hash |= (1 << (c.charCodeAt(0) - 'a'.charCodeAt(0))) |
| 106 | + out:for (const s of words) { |
| 107 | + for (const c of s) { |
| 108 | + if (((hash >> (c.charCodeAt(0) - 'a'.charCodeAt(0))) & 1) == 0) continue out |
| 109 | + } |
| 110 | + ans++ |
| 111 | + } |
| 112 | + return ans |
| 113 | +} |
| 114 | +``` |
| 115 | +Python3 代码: |
| 116 | +```Python3 |
| 117 | +class Solution: |
| 118 | + def countConsistentStrings(self, allowed: str, words: List[str]) -> int: |
| 119 | + sset = set([c for c in allowed]) |
| 120 | + ans = 0 |
| 121 | + for s in words: |
| 122 | + ok = True |
| 123 | + for c in s: |
| 124 | + if c not in sset: |
| 125 | + ok = False |
| 126 | + break |
| 127 | + ans += 1 if ok else 0 |
| 128 | + return ans |
| 129 | +``` |
| 130 | +Python3 代码: |
| 131 | +```Python3 |
| 132 | +class Solution: |
| 133 | + def countConsistentStrings(self, allowed: str, words: List[str]) -> int: |
| 134 | + num, ans = 0, 0 |
| 135 | + for c in allowed: |
| 136 | + num |= (1 << (ord(c) - ord('a'))) |
| 137 | + for s in words: |
| 138 | + ok = True |
| 139 | + for c in s: |
| 140 | + if not (num >> (ord(c) - ord('a')) & 1): |
| 141 | + ok = False |
| 142 | + break |
| 143 | + ans += 1 if ok else 0 |
| 144 | + return ans |
| 145 | +``` |
| 146 | +* 时间复杂度:$O(m + \sum_{i = 0}^{n - 1}words[i].length)$,其中 $m$ 为 `allowed` 长度,$n$ 为 `words` 长度 |
| 147 | +* 空间复杂度:$O(m)$ |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +### 最后 |
| 152 | + |
| 153 | +这是我们「刷穿 LeetCode」系列文章的第 `No.1684` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 |
| 154 | + |
| 155 | +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 |
| 156 | + |
| 157 | +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 |
| 158 | + |
| 159 | +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 |
| 160 | + |
0 commit comments