Skip to content

Commit 5ccbfdd

Browse files
authored
feat: add solutions to lc problem: No.3258 (#3431)
1 parent 4fdaa4c commit 5ccbfdd

File tree

17 files changed

+1143
-0
lines changed

17 files changed

+1143
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3258.Count%20Substrings%20That%20Satisfy%20K-Constraint%20I/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3258. 统计满足 K 约束的子字符串数量 I](https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i)
10+
11+
[English Version](/solution/3200-3299/3258.Count%20Substrings%20That%20Satisfy%20K-Constraint%20I/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个 <strong>二进制</strong> 字符串 <code>s</code> 和一个整数 <code>k</code>。</p>
18+
19+
<p>如果一个 <strong>二进制字符串</strong> 满足以下任一条件,则认为该字符串满足 <strong>k 约束</strong>:</p>
20+
21+
<ul>
22+
<li>字符串中 <code>0</code> 的数量最多为 <code>k</code>。</li>
23+
<li>字符串中 <code>1</code> 的数量最多为 <code>k</code>。</li>
24+
</ul>
25+
26+
<p>返回一个整数,表示 <code>s</code> 的所有满足 <strong>k 约束 </strong>的<span data-keyword="substring-nonempty">子字符串</span>的数量。</p>
27+
28+
<p>&nbsp;</p>
29+
30+
<p><strong class="example">示例 1:</strong></p>
31+
32+
<div class="example-block">
33+
<p><strong>输入:</strong><span class="example-io">s = "10101", k = 1</span></p>
34+
35+
<p><strong>输出:</strong><span class="example-io">12</span></p>
36+
37+
<p><strong>解释:</strong></p>
38+
39+
<p><code>s</code> 的所有子字符串中,除了 <code>"1010"</code>、<code>"10101"</code> 和 <code>"0101"</code> 外,其余子字符串都满足 k 约束。</p>
40+
</div>
41+
42+
<p><strong class="example">示例 2:</strong></p>
43+
44+
<div class="example-block">
45+
<p><strong>输入:</strong><span class="example-io">s = "1010101", k = 2</span></p>
46+
47+
<p><strong>输出:</strong><span class="example-io">25</span></p>
48+
49+
<p><strong>解释:</strong></p>
50+
51+
<p><code>s</code> 的所有子字符串中,除了长度大于 5 的子字符串外,其余子字符串都满足 k 约束。</p>
52+
</div>
53+
54+
<p><strong class="example">示例 3:</strong></p>
55+
56+
<div class="example-block">
57+
<p><strong>输入:</strong><span class="example-io">s = "11111", k = 1</span></p>
58+
59+
<p><strong>输出:</strong><span class="example-io">15</span></p>
60+
61+
<p><strong>解释:</strong></p>
62+
63+
<p><code>s</code> 的所有子字符串都满足 k 约束。</p>
64+
</div>
65+
66+
<p>&nbsp;</p>
67+
68+
<p><strong>提示:</strong></p>
69+
70+
<ul>
71+
<li><code>1 &lt;= s.length &lt;= 50</code></li>
72+
<li><code>1 &lt;= k &lt;= s.length</code></li>
73+
<li><code>s[i]</code> 是 <code>'0'</code> 或 <code>'1'</code>。</li>
74+
</ul>
75+
76+
<!-- description:end -->
77+
78+
## 解法
79+
80+
<!-- solution:start -->
81+
82+
### 方法一:滑动窗口
83+
84+
我们用两个变量 $\textit{cnt0}$ 和 $\textit{cnt1}$ 分别记录当前窗口内的 $0$ 和 $1$ 的个数,用 $\textit{ans}$ 记录满足 $k$ 约束的子字符串的个数,用 $l$ 记录窗口的左边界。
85+
86+
当我们右移窗口时,如果窗口内的 $0$ 和 $1$ 的个数都大于 $k$,我们就需要左移窗口,直到窗口内的 $0$ 和 $1$ 的个数都不大于 $k$。此时,窗口内的所有子字符串都满足 $k$ 约束,个数为 $r - l + 1$,其中 $r$ 是窗口的右边界。我们将这个个数累加到 $\textit{ans}$ 中。
87+
88+
最后,我们返回 $\textit{ans}$ 即可。
89+
90+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
91+
92+
<!-- tabs:start -->
93+
94+
#### Python3
95+
96+
```python
97+
class Solution:
98+
def countKConstraintSubstrings(self, s: str, k: int) -> int:
99+
cnt0 = cnt1 = 0
100+
ans = l = 0
101+
for r, c in enumerate(s):
102+
cnt0 += int(c) ^ 1
103+
cnt1 += int(c)
104+
while cnt0 > k and cnt1 > k:
105+
cnt0 -= int(s[l]) ^ 1
106+
cnt1 -= int(s[l])
107+
l += 1
108+
ans += r - l + 1
109+
return ans
110+
```
111+
112+
#### Java
113+
114+
```java
115+
class Solution {
116+
public int countKConstraintSubstrings(String s, int k) {
117+
int cnt0 = 0, cnt1 = 0;
118+
int ans = 0, l = 0;
119+
for (int r = 0; r < s.length(); ++r) {
120+
int x = s.charAt(r) - '0';
121+
cnt0 += x ^ 1;
122+
cnt1 += x;
123+
while (cnt0 > k && cnt1 > k) {
124+
int y = s.charAt(l++) - '0';
125+
cnt0 -= y ^ 1;
126+
cnt1 -= y;
127+
}
128+
ans += r - l + 1;
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
135+
#### C++
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
int countKConstraintSubstrings(string s, int k) {
141+
int cnt0 = 0, cnt1 = 0;
142+
int ans = 0, l = 0;
143+
for (int r = 0; r < s.length(); ++r) {
144+
int x = s[r] - '0';
145+
cnt0 += x ^ 1;
146+
cnt1 += x;
147+
while (cnt0 > k && cnt1 > k) {
148+
int y = s[l++] - '0';
149+
cnt0 -= y ^ 1;
150+
cnt1 -= y;
151+
}
152+
ans += r - l + 1;
153+
}
154+
return ans;
155+
}
156+
};
157+
```
158+
159+
#### Go
160+
161+
```go
162+
func countKConstraintSubstrings(s string, k int) (ans int) {
163+
cnt0, cnt1, l := 0, 0, 0
164+
for r, c := range s {
165+
x := int(c - '0')
166+
cnt0 += x ^ 1
167+
cnt1 += x
168+
for cnt0 > k && cnt1 > k {
169+
y := int(s[l] - '0')
170+
cnt0 -= y ^ 1
171+
cnt1 -= y
172+
l++
173+
}
174+
ans += r - l + 1
175+
}
176+
return
177+
}
178+
```
179+
180+
#### TypeScript
181+
182+
```ts
183+
function countKConstraintSubstrings(s: string, k: number): number {
184+
let [cnt0, cnt1, ans, l] = [0, 0, 0, 0];
185+
for (let r = 0; r < s.length; ++r) {
186+
const x = s[r] === '1' ? 1 : 0;
187+
cnt0 += x ^ 1;
188+
cnt1 += x;
189+
while (cnt0 > k && cnt1 > k) {
190+
const y = s[l++] === '1' ? 1 : 0;
191+
cnt0 -= y ^ 1;
192+
cnt1 -= y;
193+
}
194+
ans += r - l + 1;
195+
}
196+
return ans;
197+
}
198+
```
199+
200+
<!-- tabs:end -->
201+
202+
<!-- solution:end -->
203+
204+
<!-- problem:end -->

0 commit comments

Comments
 (0)