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 3b52bea

Browse files
feat: add solutions to lc problem: No.3258 (doocs#3711)
No.3258.Count Substrings That Satisfy K-Constraint I
1 parent fa38c56 commit 3b52bea

File tree

8 files changed

+136
-120
lines changed

8 files changed

+136
-120
lines changed

‎solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md‎

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,12 @@ tags:
101101
```python
102102
class Solution:
103103
def countKConstraintSubstrings(self, s: str, k: int) -> int:
104-
cnt0 = cnt1 =0
104+
cnt = [0, 0]
105105
ans = l = 0
106-
for r, c in enumerate(s):
107-
cnt0 += int(c) ^ 1
108-
cnt1 += int(c)
109-
while cnt0 > k and cnt1 > k:
110-
cnt0 -= int(s[l]) ^ 1
111-
cnt1 -= int(s[l])
106+
for r, x in enumerate(map(int, s)):
107+
cnt[x] += 1
108+
while cnt[0] > k and cnt[1] > k:
109+
cnt[int(s[l])] -= 1
112110
l += 1
113111
ans += r - l + 1
114112
return ans
@@ -119,16 +117,12 @@ class Solution:
119117
```java
120118
class Solution {
121119
public int countKConstraintSubstrings(String s, int k) {
122-
int cnt0 = 0, cnt1 =0;
120+
int[] cnt = newint[2];
123121
int ans = 0, l = 0;
124122
for (int r = 0; r < s.length(); ++r) {
125-
int x = s.charAt(r) - '0';
126-
cnt0 += x ^ 1;
127-
cnt1 += x;
128-
while (cnt0 > k && cnt1 > k) {
129-
int y = s.charAt(l++) - '0';
130-
cnt0 -= y ^ 1;
131-
cnt1 -= y;
123+
++cnt[s.charAt(r) - '0'];
124+
while (cnt[0] > k && cnt[1] > k) {
125+
cnt[s.charAt(l++) - '0']--;
132126
}
133127
ans += r - l + 1;
134128
}
@@ -143,16 +137,12 @@ class Solution {
143137
class Solution {
144138
public:
145139
int countKConstraintSubstrings(string s, int k) {
146-
int cnt0 = 0, cnt1 = 0;
140+
int cnt[2]{};
147141
int ans = 0, l = 0;
148142
for (int r = 0; r < s.length(); ++r) {
149-
int x = s[r] - '0';
150-
cnt0 += x ^ 1;
151-
cnt1 += x;
152-
while (cnt0 > k && cnt1 > k) {
153-
int y = s[l++] - '0';
154-
cnt0 -= y ^ 1;
155-
cnt1 -= y;
143+
cnt[s[r] - '0']++;
144+
while (cnt[0] > k && cnt[1] > k) {
145+
cnt[s[l++] - '0']--;
156146
}
157147
ans += r - l + 1;
158148
}
@@ -165,16 +155,12 @@ public:
165155
166156
```go
167157
func countKConstraintSubstrings(s string, k int) (ans int) {
168-
cnt0, cnt1, l := 0, 0, 0
158+
cnt := [2]int{}
159+
l := 0
169160
for r, c := range s {
170-
x := int(c - '0')
171-
cnt0 += x ^ 1
172-
cnt1 += x
173-
for cnt0 > k && cnt1 > k {
174-
y := int(s[l] - '0')
175-
cnt0 -= y ^ 1
176-
cnt1 -= y
177-
l++
161+
cnt[c-'0']++
162+
for ; cnt[0] > k && cnt[1] > k; l++ {
163+
cnt[s[l]-'0']--
178164
}
179165
ans += r - l + 1
180166
}
@@ -186,22 +172,43 @@ func countKConstraintSubstrings(s string, k int) (ans int) {
186172

187173
```ts
188174
function countKConstraintSubstrings(s: string, k: number): number {
189-
let [cnt0, cnt1, ans, l] = [0, 0, 0, 0];
175+
const cnt: [number, number] = [0, 0];
176+
let [ans, l] = [0, 0];
190177
for (let r = 0; r < s.length; ++r) {
191-
const x = s[r] === '1' ? 1 : 0;
192-
cnt0 += x ^ 1;
193-
cnt1 += x;
194-
while (cnt0 > k && cnt1 > k) {
195-
const y = s[l++] === '1' ? 1 : 0;
196-
cnt0 -= y ^ 1;
197-
cnt1 -= y;
178+
cnt[s.charCodeAt(r) - 48]++;
179+
while (cnt[0] > k && cnt[1] > k) {
180+
cnt[s.charCodeAt(l++) - 48]--;
198181
}
199182
ans += r - l + 1;
200183
}
201184
return ans;
202185
}
203186
```
204187

188+
#### Rust
189+
190+
```rust
191+
impl Solution {
192+
pub fn count_k_constraint_substrings(s: String, k: i32) -> i32 {
193+
let mut cnt = [0; 2];
194+
let mut l = 0;
195+
let mut ans = 0;
196+
let s = s.as_bytes();
197+
198+
for (r, &c) in s.iter().enumerate() {
199+
cnt[(c - b'0') as usize] += 1;
200+
while cnt[0] > k && cnt[1] > k {
201+
cnt[(s[l] - b'0') as usize] -= 1;
202+
l += 1;
203+
}
204+
ans += r - l + 1;
205+
}
206+
207+
ans as i32
208+
}
209+
}
210+
```
211+
205212
<!-- tabs:end -->
206213

207214
<!-- solution:end -->

‎solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md‎

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,12 @@ The time complexity is $O(n),ドル where $n$ is the length of the string $s$. The sp
9999
```python
100100
class Solution:
101101
def countKConstraintSubstrings(self, s: str, k: int) -> int:
102-
cnt0 = cnt1 =0
102+
cnt = [0, 0]
103103
ans = l = 0
104-
for r, c in enumerate(s):
105-
cnt0 += int(c) ^ 1
106-
cnt1 += int(c)
107-
while cnt0 > k and cnt1 > k:
108-
cnt0 -= int(s[l]) ^ 1
109-
cnt1 -= int(s[l])
104+
for r, x in enumerate(map(int, s)):
105+
cnt[x] += 1
106+
while cnt[0] > k and cnt[1] > k:
107+
cnt[int(s[l])] -= 1
110108
l += 1
111109
ans += r - l + 1
112110
return ans
@@ -117,16 +115,12 @@ class Solution:
117115
```java
118116
class Solution {
119117
public int countKConstraintSubstrings(String s, int k) {
120-
int cnt0 = 0, cnt1 =0;
118+
int[] cnt = newint[2];
121119
int ans = 0, l = 0;
122120
for (int r = 0; r < s.length(); ++r) {
123-
int x = s.charAt(r) - '0';
124-
cnt0 += x ^ 1;
125-
cnt1 += x;
126-
while (cnt0 > k && cnt1 > k) {
127-
int y = s.charAt(l++) - '0';
128-
cnt0 -= y ^ 1;
129-
cnt1 -= y;
121+
++cnt[s.charAt(r) - '0'];
122+
while (cnt[0] > k && cnt[1] > k) {
123+
cnt[s.charAt(l++) - '0']--;
130124
}
131125
ans += r - l + 1;
132126
}
@@ -141,16 +135,12 @@ class Solution {
141135
class Solution {
142136
public:
143137
int countKConstraintSubstrings(string s, int k) {
144-
int cnt0 = 0, cnt1 = 0;
138+
int cnt[2]{};
145139
int ans = 0, l = 0;
146140
for (int r = 0; r < s.length(); ++r) {
147-
int x = s[r] - '0';
148-
cnt0 += x ^ 1;
149-
cnt1 += x;
150-
while (cnt0 > k && cnt1 > k) {
151-
int y = s[l++] - '0';
152-
cnt0 -= y ^ 1;
153-
cnt1 -= y;
141+
cnt[s[r] - '0']++;
142+
while (cnt[0] > k && cnt[1] > k) {
143+
cnt[s[l++] - '0']--;
154144
}
155145
ans += r - l + 1;
156146
}
@@ -163,16 +153,12 @@ public:
163153
164154
```go
165155
func countKConstraintSubstrings(s string, k int) (ans int) {
166-
cnt0, cnt1, l := 0, 0, 0
156+
cnt := [2]int{}
157+
l := 0
167158
for r, c := range s {
168-
x := int(c - '0')
169-
cnt0 += x ^ 1
170-
cnt1 += x
171-
for cnt0 > k && cnt1 > k {
172-
y := int(s[l] - '0')
173-
cnt0 -= y ^ 1
174-
cnt1 -= y
175-
l++
159+
cnt[c-'0']++
160+
for ; cnt[0] > k && cnt[1] > k; l++ {
161+
cnt[s[l]-'0']--
176162
}
177163
ans += r - l + 1
178164
}
@@ -184,22 +170,43 @@ func countKConstraintSubstrings(s string, k int) (ans int) {
184170

185171
```ts
186172
function countKConstraintSubstrings(s: string, k: number): number {
187-
let [cnt0, cnt1, ans, l] = [0, 0, 0, 0];
173+
const cnt: [number, number] = [0, 0];
174+
let [ans, l] = [0, 0];
188175
for (let r = 0; r < s.length; ++r) {
189-
const x = s[r] === '1' ? 1 : 0;
190-
cnt0 += x ^ 1;
191-
cnt1 += x;
192-
while (cnt0 > k && cnt1 > k) {
193-
const y = s[l++] === '1' ? 1 : 0;
194-
cnt0 -= y ^ 1;
195-
cnt1 -= y;
176+
cnt[s.charCodeAt(r) - 48]++;
177+
while (cnt[0] > k && cnt[1] > k) {
178+
cnt[s.charCodeAt(l++) - 48]--;
196179
}
197180
ans += r - l + 1;
198181
}
199182
return ans;
200183
}
201184
```
202185

186+
#### Rust
187+
188+
```rust
189+
impl Solution {
190+
pub fn count_k_constraint_substrings(s: String, k: i32) -> i32 {
191+
let mut cnt = [0; 2];
192+
let mut l = 0;
193+
let mut ans = 0;
194+
let s = s.as_bytes();
195+
196+
for (r, &c) in s.iter().enumerate() {
197+
cnt[(c - b'0') as usize] += 1;
198+
while cnt[0] > k && cnt[1] > k {
199+
cnt[(s[l] - b'0') as usize] -= 1;
200+
l += 1;
201+
}
202+
ans += r - l + 1;
203+
}
204+
205+
ans as i32
206+
}
207+
}
208+
```
209+
203210
<!-- tabs:end -->
204211

205212
<!-- solution:end -->

‎solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.cpp‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
class Solution {
22
public:
33
int countKConstraintSubstrings(string s, int k) {
4-
int cnt0 = 0, cnt1 = 0;
4+
int cnt[2]{};
55
int ans = 0, l = 0;
66
for (int r = 0; r < s.length(); ++r) {
7-
int x = s[r] - '0';
8-
cnt0 += x ^ 1;
9-
cnt1 += x;
10-
while (cnt0 > k && cnt1 > k) {
11-
int y = s[l++] - '0';
12-
cnt0 -= y ^ 1;
13-
cnt1 -= y;
7+
cnt[s[r] - '0']++;
8+
while (cnt[0] > k && cnt[1] > k) {
9+
cnt[s[l++] - '0']--;
1410
}
1511
ans += r - l + 1;
1612
}

‎solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.go‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
func countKConstraintSubstrings(s string, k int) (ans int) {
2-
cnt0, cnt1, l := 0, 0, 0
2+
cnt := [2]int{}
3+
l := 0
34
for r, c := range s {
4-
x := int(c - '0')
5-
cnt0 += x ^ 1
6-
cnt1 += x
7-
for cnt0 > k && cnt1 > k {
8-
y := int(s[l] - '0')
9-
cnt0 -= y ^ 1
10-
cnt1 -= y
11-
l++
5+
cnt[c-'0']++
6+
for ; cnt[0] > k && cnt[1] > k; l++ {
7+
cnt[s[l]-'0']--
128
}
139
ans += r - l + 1
1410
}

‎solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.java‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
class Solution {
22
public int countKConstraintSubstrings(String s, int k) {
3-
intcnt0 = 0, cnt1 = 0;
3+
int[] cnt = newint[2];
44
int ans = 0, l = 0;
55
for (int r = 0; r < s.length(); ++r) {
6-
int x = s.charAt(r) - '0';
7-
cnt0 += x ^ 1;
8-
cnt1 += x;
9-
while (cnt0 > k && cnt1 > k) {
10-
int y = s.charAt(l++) - '0';
11-
cnt0 -= y ^ 1;
12-
cnt1 -= y;
6+
++cnt[s.charAt(r) - '0'];
7+
while (cnt[0] > k && cnt[1] > k) {
8+
cnt[s.charAt(l++) - '0']--;
139
}
1410
ans += r - l + 1;
1511
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
class Solution:
22
def countKConstraintSubstrings(self, s: str, k: int) -> int:
3-
cnt0 = cnt1=0
3+
cnt = [0, 0]
44
ans = l = 0
5-
for r, c in enumerate(s):
6-
cnt0 += int(c) ^ 1
7-
cnt1 += int(c)
8-
while cnt0 > k and cnt1 > k:
9-
cnt0 -= int(s[l]) ^ 1
10-
cnt1 -= int(s[l])
5+
for r, x in enumerate(map(int, s)):
6+
cnt[x] += 1
7+
while cnt[0] > k and cnt[1] > k:
8+
cnt[int(s[l])] -= 1
119
l += 1
1210
ans += r - l + 1
1311
return ans
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn count_k_constraint_substrings(s: String, k: i32) -> i32 {
3+
let mut cnt = [0; 2];
4+
let mut l = 0;
5+
let mut ans = 0;
6+
let s = s.as_bytes();
7+
8+
for (r, &c) in s.iter().enumerate() {
9+
cnt[(c - b'0') as usize] += 1;
10+
while cnt[0] > k && cnt[1] > k {
11+
cnt[(s[l] - b'0') as usize] -= 1;
12+
l += 1;
13+
}
14+
ans += r - l + 1;
15+
}
16+
17+
ans as i32
18+
}
19+
}

‎solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
function countKConstraintSubstrings(s: string, k: number): number {
2-
let [cnt0, cnt1, ans, l] = [0, 0, 0, 0];
2+
const cnt: [number, number] = [0, 0];
3+
let [ans, l] = [0, 0];
34
for (let r = 0; r < s.length; ++r) {
4-
const x = s[r] === '1' ? 1 : 0;
5-
cnt0 += x ^ 1;
6-
cnt1 += x;
7-
while (cnt0 > k && cnt1 > k) {
8-
const y = s[l++] === '1' ? 1 : 0;
9-
cnt0 -= y ^ 1;
10-
cnt1 -= y;
5+
cnt[s.charCodeAt(r) - 48]++;
6+
while (cnt[0] > k && cnt[1] > k) {
7+
cnt[s.charCodeAt(l++) - 48]--;
118
}
129
ans += r - l + 1;
1310
}

0 commit comments

Comments
(0)

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