-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2450-NumberOfDistinctBinaryStringsAfterApplyingOperations.go
More file actions
68 lines (58 loc) · 2.67 KB
/
2450-NumberOfDistinctBinaryStringsAfterApplyingOperations.go
File metadata and controls
68 lines (58 loc) · 2.67 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
package main
// 2450. Number of Distinct Binary Strings After Applying Operations
// You are given a binary string s and a positive integer k.
// You can apply the following operation on the string any number of times:
// Choose any substring of size k from s and flip all its characters,
// that is, turn all 1's into 0's, and all 0's into 1's.
// Return the number of distinct strings you can obtain.
// Since the answer may be too large, return it modulo 10^9 + 7.
// Note that:
// A binary string is a string that consists only of the characters 0 and 1.
// A substring is a contiguous part of a string.
// Example 1:
// Input: s = "1001", k = 3
// Output: 4
// Explanation: We can obtain the following strings:
// - Applying no operation on the string gives s = "1001".
// - Applying one operation on the substring starting at index 0 gives s = "0111".
// - Applying one operation on the substring starting at index 1 gives s = "1110".
// - Applying one operation on both the substrings starting at indices 0 and 1 gives s = "0000".
// It can be shown that we cannot obtain any other string, so the answer is 4.
// Example 2:
// Input: s = "10110", k = 5
// Output: 2
// Explanation: We can obtain the following strings:
// - Applying no operation on the string gives s = "10110".
// - Applying one operation on the whole string gives s = "01001".
// It can be shown that we cannot obtain any other string, so the answer is 2.
// Constraints:
// 1 <= k <= s.length <= 10^5
// s[i] is either 0 or 1.
import "fmt"
func countDistinctStrings(s string, k int) int {
res, mod := 2, 1_000_000_007
for i := 1; i <= len(s) - k; i++ {
res = (2 * res) % mod
}
return res
}
func main() {
// Example 1:
// Input: s = "1001", k = 3
// Output: 4
// Explanation: We can obtain the following strings:
// - Applying no operation on the string gives s = "1001".
// - Applying one operation on the substring starting at index 0 gives s = "0111".
// - Applying one operation on the substring starting at index 1 gives s = "1110".
// - Applying one operation on both the substrings starting at indices 0 and 1 gives s = "0000".
// It can be shown that we cannot obtain any other string, so the answer is 4.
fmt.Println(countDistinctStrings("1001", 3)) // 4
// Example 2:
// Input: s = "10110", k = 5
// Output: 2
// Explanation: We can obtain the following strings:
// - Applying no operation on the string gives s = "10110".
// - Applying one operation on the whole string gives s = "01001".
// It can be shown that we cannot obtain any other string, so the answer is 2.
fmt.Println(countDistinctStrings("10110", 5)) // 2
}