-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3258-CountSubstringsThatSatisfyKConstraintI.go
More file actions
78 lines (68 loc) · 2.21 KB
/
3258-CountSubstringsThatSatisfyKConstraintI.go
File metadata and controls
78 lines (68 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
// 3258. Count Substrings That Satisfy K-Constraint I
// You are given a binary string s and an integer k.
// A binary string satisfies the k-constraint if either of the following conditions holds:
// The number of 0's in the string is at most k.
// The number of 1's in the string is at most k.
// Return an integer denoting the number of substrings of s that satisfy the k-constraint.
// Example 1:
// Input: s = "10101", k = 1
// Output: 12
// Explanation:
// Every substring of s except the substrings "1010", "10101", and "0101" satisfies the k-constraint.
// Example 2:
// Input: s = "1010101", k = 2
// Output: 25
// Explanation:
// Every substring of s except the substrings with a length greater than 5 satisfies the k-constraint.
// Example 3:
// Input: s = "11111", k = 1
// Output: 15
// Explanation:
// All substrings of s satisfy the k-constraint.
// Constraints:
// 1 <= s.length <= 50
// 1 <= k <= s.length
// s[i] is either '0' or '1'.
import "fmt"
// Sliding Window
func countKConstraintSubstrings(s string, k int) int {
res, count0, count1, left := 0, 0, 0, -1
for right := 0; right < len(s); right++ {
if s[right] == '1' {
count1++
} else {
count0++
}
for left <= right && count0 > k && count1> k {
left++
if s[left] == '1' {
count1--
} else {
count0--
}
}
res += right - left
}
return res
}
func main() {
// Example 1:
// Input: s = "10101", k = 1
// Output: 12
// Explanation:
// Every substring of s except the substrings "1010", "10101", and "0101" satisfies the k-constraint.
fmt.Println(countKConstraintSubstrings("10101", 1)) // 12
// Example 2:
// Input: s = "1010101", k = 2
// Output: 25
// Explanation:
// Every substring of s except the substrings with a length greater than 5 satisfies the k-constraint.
fmt.Println(countKConstraintSubstrings("1010101", 2)) // 25
// Example 3:
// Input: s = "11111", k = 1
// Output: 15
// Explanation:
// All substrings of s satisfy the k-constraint.
fmt.Println(countKConstraintSubstrings("11111", 1)) // 15
}