diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md index 1364d8ed9ce0c..3571085f71978 100644 --- a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README.md @@ -101,14 +101,12 @@ tags: ```python class Solution: def countKConstraintSubstrings(self, s: str, k: int) -> int: - cnt0 = cnt1 = 0 + cnt = [0, 0] ans = l = 0 - for r, c in enumerate(s): - cnt0 += int(c) ^ 1 - cnt1 += int(c) - while cnt0 > k and cnt1 > k: - cnt0 -= int(s[l]) ^ 1 - cnt1 -= int(s[l]) + for r, x in enumerate(map(int, s)): + cnt[x] += 1 + while cnt[0] > k and cnt[1] > k: + cnt[int(s[l])] -= 1 l += 1 ans += r - l + 1 return ans @@ -119,16 +117,12 @@ class Solution: ```java class Solution { public int countKConstraintSubstrings(String s, int k) { - int cnt0 = 0, cnt1 = 0; + int[] cnt = new int[2]; int ans = 0, l = 0; for (int r = 0; r < s.length(); ++r) { - int x = s.charAt(r) - '0'; - cnt0 += x ^ 1; - cnt1 += x; - while (cnt0 > k && cnt1 > k) { - int y = s.charAt(l++) - '0'; - cnt0 -= y ^ 1; - cnt1 -= y; + ++cnt[s.charAt(r) - '0']; + while (cnt[0] > k && cnt[1] > k) { + cnt[s.charAt(l++) - '0']--; } ans += r - l + 1; } @@ -143,16 +137,12 @@ class Solution { class Solution { public: int countKConstraintSubstrings(string s, int k) { - int cnt0 = 0, cnt1 = 0; + int cnt[2]{}; int ans = 0, l = 0; for (int r = 0; r < s.length(); ++r) { - int x = s[r] - '0'; - cnt0 += x ^ 1; - cnt1 += x; - while (cnt0 > k && cnt1 > k) { - int y = s[l++] - '0'; - cnt0 -= y ^ 1; - cnt1 -= y; + cnt[s[r] - '0']++; + while (cnt[0] > k && cnt[1] > k) { + cnt[s[l++] - '0']--; } ans += r - l + 1; } @@ -165,16 +155,12 @@ public: ```go func countKConstraintSubstrings(s string, k int) (ans int) { - cnt0, cnt1, l := 0, 0, 0 + cnt := [2]int{} + l := 0 for r, c := range s { - x := int(c - '0') - cnt0 += x ^ 1 - cnt1 += x - for cnt0 > k && cnt1 > k { - y := int(s[l] - '0') - cnt0 -= y ^ 1 - cnt1 -= y - l++ + cnt[c-'0']++ + for ; cnt[0] > k && cnt[1] > k; l++ { + cnt[s[l]-'0']-- } ans += r - l + 1 } @@ -186,15 +172,12 @@ func countKConstraintSubstrings(s string, k int) (ans int) { ```ts function countKConstraintSubstrings(s: string, k: number): number { - let [cnt0, cnt1, ans, l] = [0, 0, 0, 0]; + const cnt: [number, number] = [0, 0]; + let [ans, l] = [0, 0]; for (let r = 0; r < s.length; ++r) { - const x = s[r] === '1' ? 1 : 0; - cnt0 += x ^ 1; - cnt1 += x; - while (cnt0 > k && cnt1 > k) { - const y = s[l++] === '1' ? 1 : 0; - cnt0 -= y ^ 1; - cnt1 -= y; + cnt[s.charCodeAt(r) - 48]++; + while (cnt[0] > k && cnt[1] > k) { + cnt[s.charCodeAt(l++) - 48]--; } ans += r - l + 1; } @@ -202,6 +185,30 @@ function countKConstraintSubstrings(s: string, k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_k_constraint_substrings(s: String, k: i32) -> i32 { + let mut cnt = [0; 2]; + let mut l = 0; + let mut ans = 0; + let s = s.as_bytes(); + + for (r, &c) in s.iter().enumerate() { + cnt[(c - b'0') as usize] += 1; + while cnt[0] > k && cnt[1] > k { + cnt[(s[l] - b'0') as usize] -= 1; + l += 1; + } + ans += r - l + 1; + } + + ans as i32 + } +} +``` + diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md index 2299434d843da..c5e7a28f4fbde 100644 --- a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/README_EN.md @@ -99,14 +99,12 @@ The time complexity is $O(n)$, where $n$ is the length of the string $s$. The sp ```python class Solution: def countKConstraintSubstrings(self, s: str, k: int) -> int: - cnt0 = cnt1 = 0 + cnt = [0, 0] ans = l = 0 - for r, c in enumerate(s): - cnt0 += int(c) ^ 1 - cnt1 += int(c) - while cnt0 > k and cnt1 > k: - cnt0 -= int(s[l]) ^ 1 - cnt1 -= int(s[l]) + for r, x in enumerate(map(int, s)): + cnt[x] += 1 + while cnt[0] > k and cnt[1] > k: + cnt[int(s[l])] -= 1 l += 1 ans += r - l + 1 return ans @@ -117,16 +115,12 @@ class Solution: ```java class Solution { public int countKConstraintSubstrings(String s, int k) { - int cnt0 = 0, cnt1 = 0; + int[] cnt = new int[2]; int ans = 0, l = 0; for (int r = 0; r < s.length(); ++r) { - int x = s.charAt(r) - '0'; - cnt0 += x ^ 1; - cnt1 += x; - while (cnt0 > k && cnt1 > k) { - int y = s.charAt(l++) - '0'; - cnt0 -= y ^ 1; - cnt1 -= y; + ++cnt[s.charAt(r) - '0']; + while (cnt[0] > k && cnt[1] > k) { + cnt[s.charAt(l++) - '0']--; } ans += r - l + 1; } @@ -141,16 +135,12 @@ class Solution { class Solution { public: int countKConstraintSubstrings(string s, int k) { - int cnt0 = 0, cnt1 = 0; + int cnt[2]{}; int ans = 0, l = 0; for (int r = 0; r < s.length(); ++r) { - int x = s[r] - '0'; - cnt0 += x ^ 1; - cnt1 += x; - while (cnt0 > k && cnt1 > k) { - int y = s[l++] - '0'; - cnt0 -= y ^ 1; - cnt1 -= y; + cnt[s[r] - '0']++; + while (cnt[0] > k && cnt[1] > k) { + cnt[s[l++] - '0']--; } ans += r - l + 1; } @@ -163,16 +153,12 @@ public: ```go func countKConstraintSubstrings(s string, k int) (ans int) { - cnt0, cnt1, l := 0, 0, 0 + cnt := [2]int{} + l := 0 for r, c := range s { - x := int(c - '0') - cnt0 += x ^ 1 - cnt1 += x - for cnt0 > k && cnt1 > k { - y := int(s[l] - '0') - cnt0 -= y ^ 1 - cnt1 -= y - l++ + cnt[c-'0']++ + for ; cnt[0] > k && cnt[1] > k; l++ { + cnt[s[l]-'0']-- } ans += r - l + 1 } @@ -184,15 +170,12 @@ func countKConstraintSubstrings(s string, k int) (ans int) { ```ts function countKConstraintSubstrings(s: string, k: number): number { - let [cnt0, cnt1, ans, l] = [0, 0, 0, 0]; + const cnt: [number, number] = [0, 0]; + let [ans, l] = [0, 0]; for (let r = 0; r < s.length; ++r) { - const x = s[r] === '1' ? 1 : 0; - cnt0 += x ^ 1; - cnt1 += x; - while (cnt0 > k && cnt1 > k) { - const y = s[l++] === '1' ? 1 : 0; - cnt0 -= y ^ 1; - cnt1 -= y; + cnt[s.charCodeAt(r) - 48]++; + while (cnt[0] > k && cnt[1] > k) { + cnt[s.charCodeAt(l++) - 48]--; } ans += r - l + 1; } @@ -200,6 +183,30 @@ function countKConstraintSubstrings(s: string, k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_k_constraint_substrings(s: String, k: i32) -> i32 { + let mut cnt = [0; 2]; + let mut l = 0; + let mut ans = 0; + let s = s.as_bytes(); + + for (r, &c) in s.iter().enumerate() { + cnt[(c - b'0') as usize] += 1; + while cnt[0] > k && cnt[1] > k { + cnt[(s[l] - b'0') as usize] -= 1; + l += 1; + } + ans += r - l + 1; + } + + ans as i32 + } +} +``` + diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.cpp b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.cpp index 0b90f0c7816b6..16d498751de9b 100644 --- a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.cpp +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.cpp @@ -1,16 +1,12 @@ class Solution { public: int countKConstraintSubstrings(string s, int k) { - int cnt0 = 0, cnt1 = 0; + int cnt[2]{}; int ans = 0, l = 0; for (int r = 0; r < s.length(); ++r) { - int x = s[r] - '0'; - cnt0 += x ^ 1; - cnt1 += x; - while (cnt0 > k && cnt1 > k) { - int y = s[l++] - '0'; - cnt0 -= y ^ 1; - cnt1 -= y; + cnt[s[r] - '0']++; + while (cnt[0] > k && cnt[1] > k) { + cnt[s[l++] - '0']--; } ans += r - l + 1; } diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.go b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.go index fa3e015156a13..3b735116dcfbb 100644 --- a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.go +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.go @@ -1,14 +1,10 @@ func countKConstraintSubstrings(s string, k int) (ans int) { - cnt0, cnt1, l := 0, 0, 0 + cnt := [2]int{} + l := 0 for r, c := range s { - x := int(c - '0') - cnt0 += x ^ 1 - cnt1 += x - for cnt0 > k && cnt1 > k { - y := int(s[l] - '0') - cnt0 -= y ^ 1 - cnt1 -= y - l++ + cnt[c-'0']++ + for ; cnt[0] > k && cnt[1] > k; l++ { + cnt[s[l]-'0']-- } ans += r - l + 1 } diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.java b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.java index e5a6fe9aef08a..b36859a8a8a4f 100644 --- a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.java +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.java @@ -1,15 +1,11 @@ class Solution { public int countKConstraintSubstrings(String s, int k) { - int cnt0 = 0, cnt1 = 0; + int[] cnt = new int[2]; int ans = 0, l = 0; for (int r = 0; r < s.length(); ++r) { - int x = s.charAt(r) - '0'; - cnt0 += x ^ 1; - cnt1 += x; - while (cnt0 > k && cnt1 > k) { - int y = s.charAt(l++) - '0'; - cnt0 -= y ^ 1; - cnt1 -= y; + ++cnt[s.charAt(r) - '0']; + while (cnt[0] > k && cnt[1] > k) { + cnt[s.charAt(l++) - '0']--; } ans += r - l + 1; } diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.py b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.py index 94dd71f835f60..234849fe5e561 100644 --- a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.py +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.py @@ -1,13 +1,11 @@ class Solution: def countKConstraintSubstrings(self, s: str, k: int) -> int: - cnt0 = cnt1 = 0 + cnt = [0, 0] ans = l = 0 - for r, c in enumerate(s): - cnt0 += int(c) ^ 1 - cnt1 += int(c) - while cnt0 > k and cnt1 > k: - cnt0 -= int(s[l]) ^ 1 - cnt1 -= int(s[l]) + for r, x in enumerate(map(int, s)): + cnt[x] += 1 + while cnt[0] > k and cnt[1] > k: + cnt[int(s[l])] -= 1 l += 1 ans += r - l + 1 return ans diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.rs b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.rs new file mode 100644 index 0000000000000..8dc2762dec9b5 --- /dev/null +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.rs @@ -0,0 +1,19 @@ +impl Solution { + pub fn count_k_constraint_substrings(s: String, k: i32) -> i32 { + let mut cnt = [0; 2]; + let mut l = 0; + let mut ans = 0; + let s = s.as_bytes(); + + for (r, &c) in s.iter().enumerate() { + cnt[(c - b'0') as usize] += 1; + while cnt[0] > k && cnt[1] > k { + cnt[(s[l] - b'0') as usize] -= 1; + l += 1; + } + ans += r - l + 1; + } + + ans as i32 + } +} diff --git a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts index b888e706cd966..d25347e7b260d 100644 --- a/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts +++ b/solution/3200-3299/3258.Count Substrings That Satisfy K-Constraint I/Solution.ts @@ -1,13 +1,10 @@ function countKConstraintSubstrings(s: string, k: number): number { - let [cnt0, cnt1, ans, l] = [0, 0, 0, 0]; + const cnt: [number, number] = [0, 0]; + let [ans, l] = [0, 0]; for (let r = 0; r < s.length; ++r) { - const x = s[r] === '1' ? 1 : 0; - cnt0 += x ^ 1; - cnt1 += x; - while (cnt0 > k && cnt1 > k) { - const y = s[l++] === '1' ? 1 : 0; - cnt0 -= y ^ 1; - cnt1 -= y; + cnt[s.charCodeAt(r) - 48]++; + while (cnt[0] > k && cnt[1] > k) { + cnt[s.charCodeAt(l++) - 48]--; } ans += r - l + 1; }