Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2102dba

Browse files
Merge pull request SharingSource#738 from SharingSource/ac_oier
✨feat: add 1684
2 parents d4f836a + 452fb13 commit 2102dba

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
| [1662. 检查两个字符串数组是否相等](https://leetcode.cn/problems/check-if-two-string-arrays-are-equivalent/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-if-two-string-arrays-are-equivalent/solution/by-ac_oier-h0l6/) | 简单 | 🤩🤩🤩🤩 |
192192
| [1672. 最富有客户的资产总量](https://leetcode-cn.com/problems/richest-customer-wealth/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/richest-customer-wealth/solution/by-ac_oier-ai19/) | 简单 | 🤩🤩🤩🤩 |
193193
| [1678. 设计 Goal 解析器](https://leetcode.cn/problems/goal-parser-interpretation/) | [LeetCode 题解链接](https://leetcode.cn/problems/goal-parser-interpretation/solution/by-ac_oier-a00y/) | 简单 | 🤩🤩🤩🤩 |
194+
| [1684. 统计一致字符串的数目](https://leetcode.cn/problems/count-the-number-of-consistent-strings/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-the-number-of-consistent-strings/solution/by-ac_oier-j2kj/) | 简单 | 🤩🤩🤩🤩 |
194195
| [1688. 比赛中的配对次数](https://leetcode-cn.com/problems/count-of-matches-in-tournament/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-of-matches-in-tournament/solution/gong-shui-san-xie-jian-dan-nao-jin-ji-zh-cx7a/) | 简单 | 🤩🤩🤩🤩🤩 |
195196
| [1694. 重新格式化电话号码](https://leetcode.cn/problems/reformat-phone-number/) | [LeetCode 题解链接](https://leetcode.cn/problems/reformat-phone-number/solution/by-ac_oier-82xq/) | 简单 | 🤩🤩🤩🤩 |
196197
| [1700. 无法吃午餐的学生数量](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/) | [LeetCode 题解链接](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/solution/by-ac_oier-rvc3/) | 简单 | 🤩🤩🤩🤩🤩 |
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /