-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-of-ways-to-split-a-string.go
74 lines (62 loc) · 1.12 KB
/
number-of-ways-to-split-a-string.go
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
package main
import (
"fmt"
)
// source: https://leetcode.com/problems/number-of-ways-to-split-a-string/
func numWays(s string) int {
const mod = 1000000007
var onesCnt, onesInStr int
for _, r := range s {
if r == '1' {
onesCnt++
}
}
if onesCnt%3 != 0 {
return 0
}
if onesCnt == 0 {
return ((len(s) - 1) * (len(s) - 2) / 2) % mod
}
onesInStr = onesCnt / 3
var l, r = 0, len(s) - 1
for s[l] == '0' {
l++
}
for s[r] == '0' {
r--
}
s = s[l : r+1]
var res = 1
var i = 0
for i < len(s) {
freeZerosCnt := 1
curOnesCnt := 0
for curOnesCnt < onesInStr {
for s[i] != '1' {
i++
}
curOnesCnt++
i++
}
for i < len(s) && s[i] != '1' {
freeZerosCnt++
i++
}
res *= freeZerosCnt
}
return res % mod
}
func main() {
// Example 4
var s4 string = "100100010100110"
fmt.Println("Expected: 12 Output: ", numWays(s4))
// Example 1
var s1 string = "10101"
fmt.Println("Expected: 4 Output: ", numWays(s1))
// Example 2
var s2 string = "1001"
fmt.Println("Expected: 0 Output: ", numWays(s2))
// Example 3
var s3 string = "0000"
fmt.Println("Expected: 3 Output: ", numWays(s3))
}