Skip to content

Commit 8472d91

Browse files
authored
Merge pull request #617 from LetMeFly666/3258
添加问题“3258.统计满足K约束的子字符串数量I”的代码和题解
2 parents 8ad039b + dce5ae3 commit 8472d91

11 files changed

+334
-90
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-12 18:08:51
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-12 18:25:58
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countKConstraintSubstrings(string s, int k) {
14+
int cnt[2] = {0};
15+
int ans = 0;
16+
for (int l = 0, r = 0; r < s.size(); r++) {
17+
cnt[s[r] - '0']++;
18+
while (cnt[0] > k && cnt[1] > k) { // 啊这,De了半天原来是“任一”
19+
cnt[s[l++] - '0']--;
20+
}
21+
ans += r - l + 1;
22+
}
23+
return ans;
24+
}
25+
};
26+
27+
#ifdef _WIN32
28+
int main() {
29+
string s = "10101";
30+
int k = 1;
31+
Solution sol;
32+
cout << sol.countKConstraintSubstrings(s, k) << endl;
33+
return 0;
34+
}
35+
#endif
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-12 18:14:45
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-12 18:33:51
6+
*/
7+
package main
8+
9+
func countKConstraintSubstrings(s string, k int) (ans int) {
10+
cnt := make([]int, 2)
11+
for l, r := 0, 0; r < len(s); r++ {
12+
cnt[s[r] - '0']++
13+
for cnt[0] > k && cnt[1] > k {
14+
cnt[s[l] - '0']--
15+
l++
16+
}
17+
ans += r - l + 1
18+
}
19+
return
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-12 18:14:43
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-12 18:31:42
6+
*/
7+
class Solution {
8+
public int countKConstraintSubstrings(String s, int k) {
9+
int[] cnt = new int[2];
10+
int ans = 0;
11+
for (int l = 0, r = 0; r < s.length(); r++) {
12+
cnt[s.charAt(r) - '0']++;
13+
while (cnt[0] > k && cnt[1] > k) {
14+
cnt[s.charAt(l++) - '0']--;
15+
}
16+
ans += r - l + 1;
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-11-12 18:14:40
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-11-12 18:29:13
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def countKConstraintSubstrings(self, s: str, k: int) -> int:
11+
cnt = [0, 0]
12+
ans = 0
13+
l = 0
14+
for r in range(len(s)):
15+
cnt[ord(s[r]) - ord('0')] += 1
16+
while cnt[0] > k and cnt[1] > k:
17+
cnt[ord(s[l]) - ord('0')] -= 1
18+
l += 1
19+
ans += r - l + 1
20+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@
721721
|3242.设计相邻元素求和服务|简单|<a href="https://leetcode.cn/problems/design-neighbor-sum-service/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/11/LeetCode%203242.%E8%AE%BE%E8%AE%A1%E7%9B%B8%E9%82%BB%E5%85%83%E7%B4%A0%E6%B1%82%E5%92%8C%E6%9C%8D%E5%8A%A1/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143698347" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/design-neighbor-sum-service/solutions/2985408/letmefly-3242she-ji-xiang-lin-yuan-su-qi-mc7m/" target="_blank">LeetCode题解</a>|
722722
|3254.长度为K的子数组的能量值I|中等|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/06/LeetCode%203254.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143575677" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-i/solutions/2979562/letmefly-3254chang-du-wei-k-de-zi-shu-zu-iu8u/" target="_blank">LeetCode题解</a>|
723723
|3255.长度为K的子数组的能量值II|中等|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/07/LeetCode%203255.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143591327" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/solutions/2980432/letmefly-3255chang-du-wei-k-de-zi-shu-zu-rags/" target="_blank">LeetCode题解</a>|
724+
|3258.统计满足K约束的子字符串数量I|简单|<a href="https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/12/LeetCode%203258.%E7%BB%9F%E8%AE%A1%E6%BB%A1%E8%B6%B3K%E7%BA%A6%E6%9D%9F%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0%E9%87%8FI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143726399" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i/solutions/2986598/letmefly-3258tong-ji-man-zu-k-yue-shu-de-fs44/" target="_blank">LeetCode题解</a>|
724725
|3259.超级饮料的最大强化能量|中等|<a href="https://leetcode.cn/problems/maximum-energy-boost-from-two-drinks/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/01/LeetCode%203259.%E8%B6%85%E7%BA%A7%E9%A5%AE%E6%96%99%E7%9A%84%E6%9C%80%E5%A4%A7%E5%BC%BA%E5%8C%96%E8%83%BD%E9%87%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143429899" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-energy-boost-from-two-drinks/solutions/2973620/letmefly-3259chao-ji-yin-liao-de-zui-da-eeusa/" target="_blank">LeetCode题解</a>|
725726
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
726727
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
title: 3258.统计满足 K 约束的子字符串数量 I
3+
date: 2024-11-12 18:35:49
4+
tags: [题解, LeetCode, 简单, 字符串, 滑动窗口]
5+
---
6+
7+
# 【LetMeFly】3258.统计满足 K 约束的子字符串数量 I:滑动窗口(硬卷O(n))
8+
9+
力扣题目链接:[https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i/](https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i/)
10+
11+
<p>给你一个 <strong>二进制</strong> 字符串 <code>s</code> 和一个整数 <code>k</code>。</p>
12+
13+
<p>如果一个 <strong>二进制字符串</strong> 满足以下任一条件,则认为该字符串满足 <strong>k 约束</strong>:</p>
14+
15+
<ul>
16+
<li>字符串中 <code>0</code> 的数量最多为 <code>k</code>。</li>
17+
<li>字符串中 <code>1</code> 的数量最多为 <code>k</code>。</li>
18+
</ul>
19+
20+
<p>返回一个整数,表示 <code>s</code> 的所有满足 <strong>k 约束 </strong>的<span data-keyword="substring-nonempty">子字符串</span>的数量。</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong class="example">示例 1:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>输入:</strong><span class="example-io">s = "10101", k = 1</span></p>
28+
29+
<p><strong>输出:</strong><span class="example-io">12</span></p>
30+
31+
<p><strong>解释:</strong></p>
32+
33+
<p><code>s</code> 的所有子字符串中,除了 <code>"1010"</code>、<code>"10101"</code> 和 <code>"0101"</code> 外,其余子字符串都满足 k 约束。</p>
34+
</div>
35+
36+
<p><strong class="example">示例 2:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>输入:</strong><span class="example-io">s = "1010101", k = 2</span></p>
40+
41+
<p><strong>输出:</strong><span class="example-io">25</span></p>
42+
43+
<p><strong>解释:</strong></p>
44+
45+
<p><code>s</code> 的所有子字符串中,除了长度大于 5 的子字符串外,其余子字符串都满足 k 约束。</p>
46+
</div>
47+
48+
<p><strong class="example">示例 3:</strong></p>
49+
50+
<div class="example-block">
51+
<p><strong>输入:</strong><span class="example-io">s = "11111", k = 1</span></p>
52+
53+
<p><strong>输出:</strong><span class="example-io">15</span></p>
54+
55+
<p><strong>解释:</strong></p>
56+
57+
<p><code>s</code> 的所有子字符串都满足 k 约束。</p>
58+
</div>
59+
60+
<p>&nbsp;</p>
61+
62+
<p><strong>提示:</strong></p>
63+
64+
<ul>
65+
<li><code>1 &lt;= s.length &lt;= 50</code></li>
66+
<li><code>1 &lt;= k &lt;= s.length</code></li>
67+
<li><code>s[i]</code> 是 <code>'0'</code> 或 <code>'1'</code>。</li>
68+
</ul>
69+
70+
71+
72+
## 解题方法:滑动窗口
73+
74+
使用两个遍历`cnt[0]``cnt[1]`分别记录当前“窗口”中`0``1`的数量,使用两个指针`l``r`分别代表窗口的始末下标。
75+
76+
每次窗口右指针`r`向右移动一位,如果这个移动导致窗口中`cnt[0] > k``cnt[1] > k`,则不断右移左指针`l`直至窗口中子字符串符合“K约束”要求。
77+
78+
对于一个窗口,我们累加以`r`结尾的子字符串数量:$r - l + 1$。
79+
80+
+ 时间复杂度$O(len(s))$
81+
+ 空间复杂度$O(1)$
82+
83+
### AC代码
84+
85+
#### C++
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
int countKConstraintSubstrings(string s, int k) {
91+
int cnt[2] = {0};
92+
int ans = 0;
93+
for (int l = 0, r = 0; r < s.size(); r++) {
94+
cnt[s[r] - '0']++;
95+
while (cnt[0] > k && cnt[1] > k) { // 啊这,De了半天原来是“任一”
96+
cnt[s[l++] - '0']--;
97+
}
98+
ans += r - l + 1;
99+
}
100+
return ans;
101+
}
102+
};
103+
```
104+
105+
#### Python
106+
107+
```python
108+
from typing import List
109+
110+
class Solution:
111+
def countKConstraintSubstrings(self, s: str, k: int) -> int:
112+
cnt = [0, 0]
113+
ans = 0
114+
l = 0
115+
for r in range(len(s)):
116+
cnt[ord(s[r]) - ord('0')] += 1
117+
while cnt[0] > k and cnt[1] > k:
118+
cnt[ord(s[l]) - ord('0')] -= 1
119+
l += 1
120+
ans += r - l + 1
121+
return ans
122+
```
123+
124+
#### Java
125+
126+
```java
127+
class Solution {
128+
public int countKConstraintSubstrings(String s, int k) {
129+
int[] cnt = new int[2];
130+
int ans = 0;
131+
for (int l = 0, r = 0; r < s.length(); r++) {
132+
cnt[s.charAt(r) - '0']++;
133+
while (cnt[0] > k && cnt[1] > k) {
134+
cnt[s.charAt(l++) - '0']--;
135+
}
136+
ans += r - l + 1;
137+
}
138+
return ans;
139+
}
140+
}
141+
```
142+
143+
#### Go
144+
145+
```go
146+
package main
147+
148+
func countKConstraintSubstrings(s string, k int) (ans int) {
149+
cnt := make([]int, 2)
150+
for l, r := 0, 0; r < len(s); r++ {
151+
cnt[s[r] - '0']++
152+
for cnt[0] > k && cnt[1] > k {
153+
cnt[s[l] - '0']--
154+
l++
155+
}
156+
ans += r - l + 1
157+
}
158+
return
159+
}
160+
```
161+
162+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/11/12/LeetCode%203258.%E7%BB%9F%E8%AE%A1%E6%BB%A1%E8%B6%B3K%E7%BA%A6%E6%9D%9F%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0%E9%87%8FI/)哦~
163+
>
164+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/143726399](https://letmefly.blog.csdn.net/article/details/143726399)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ tags: [其他, 知识, 英语, Notes]
671671
|annex|v. 附加,附带,并吞,抢占,获得<br/>n. 附件,附属品,附属建筑|
672672
|wade|v. 跋涉,趟水|
673673
|||
674-
|perturbed|adj. 焦虑的,不安的,烦躁的<br/>v. 使焦虑,使不安|
674+
|<font color="#28bea0" title="二次复习">perturbed</font>|adj. 焦虑的,不安的,烦躁的<br/>v. 使焦虑,使不安|
675675
|garrison|n. 卫戍部队,守备不对,卫戍区,驻防地<br/>v. 驻防,派(兵)驻守|
676676
|woodpecker|n. 啄木鸟|
677677
|<font color="#28bea0" title="二次复习">cosmetics</font>|n. 化妆品|
@@ -777,6 +777,11 @@ tags: [其他, 知识, 英语, Notes]
777777
|materialism|n. 唯物主义,唯物论,实利主义,物质主义|
778778
|deposition|n. 沉积作用,沉积物,罢免,废黜(chù),证词,证言|
779779
|bullion|n. 大量的金/银,金/银条|
780+
|||
781+
|herewith|adv. 随同此信/书/文件<details><summary>例句</summary>I enclose herewith a copy of the report for your information.<br/>我随函附上这份报告供您参考。</details>|
782+
|prophet|n. 预言者,(宗教里的)先知,提倡者|
783+
|panoramic|adj. 全景的|
784+
|craziness|n. 疯狂|
780785

781786
<p class="wordCounts">单词收录总数</p>
782787

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,9 @@ xx型
539539
|げんき|很好|
540540
|||
541541
|せなか|背部|
542-
||眼睛|
542+
|目(め)|眼睛|
543543
|みみ|耳朵|
544+
|鼻(はな)|鼻子|
544545
|お腹(おなか)|腹部|
545546
|頭(あたま)||
546547
|あし||
@@ -552,6 +553,8 @@ xx型
552553
|病気(びょうき)|疾病|
553554
|風邪(かぜ)|感冒|
554555
|||
556+
|ふと|胖的|
557+
|||
555558
|夏(なつ)|夏天|
556559
|冬(ふゆ)|冬天|
557560
|||
@@ -725,6 +728,7 @@ xx型
725728
|<span title="看超炮的时候学到的汉字">有難う</span>(ありがとう)|谢谢|
726729
|答え(こたえ)|回答|
727730
|ので|因为|
731+
|すぎ||
728732
|||
729733
|牛肉(ぎゅうにく)|牛肉|
730734
|豚肉(ぶたにく)|猪肉|
@@ -1046,6 +1050,9 @@ xx型
10461050
|煙草は体に悪いです。<br/>烟对身体不好。|
10471051
|たくさんスポーツ<font color="#28bea0">した</font>ほうがいいです。<br/><details><summary>最好XX</summary>日语用<font color="#ce82ff">ほうがいい</font>表示最好干某事,<br/><font color="#ce82ff">ほうがいい</font>前用动词<font color="#ce82ff">た形</font>。<br/>词形变化和<font color="#ce82ff">て形</font>一样,<br/>把<font color="#ce82ff">て</font>/<font color="#ce82ff">で</font>变成<font color="#ce82ff">た</font>/<font color="#ce82ff">だ</font>就可以了。</details>|
10481052
|昨日彼女は学校を休んだと思います。<br/>觉得她昨天没上学。|
1053+
|犬は鼻がいいです。<br/>狗鼻子很灵。|
1054+
|また食べすぎました。<br/>又吃多了。|
1055+
|田中さんはふとっていますか?<br/>田中先生胖吗?|
10491056

10501057
[单词添加时间](https://github.com/LetMeFly666/LeetCode/blame/master/Solutions/Other-Japanese-LearningNotes.md)
10511058

0 commit comments

Comments
 (0)