-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathnumber-of-ways-to-split-a-string.py
40 lines (28 loc) · 1.11 KB
/
number-of-ways-to-split-a-string.py
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
import math
class Solution:
def numWays(self, string: str) -> int:
ones_total = string.count("1")
if ones_total == 0:
return (
math.factorial(len(string) - 1)
// 2
// (math.factorial(len(string) - 3))
) % (10 ** 9 + 7)
if ones_total % 3 != 0:
return 0
sep_left_start, sep_left_end = len(string), len(string)
sep_right_start, sep_right_end = len(string), len(string)
ones = 0
for pos in range(len(string)):
ones += string[pos] == "1"
if ones == ones_total // 3:
sep_left_start = min(sep_left_start, pos)
if ones == ones_total // 3 + 1:
sep_left_end = min(sep_left_end, pos)
if ones == (ones_total // 3) * 2:
sep_right_start = min(sep_right_start, pos)
if ones == (ones_total // 3) * 2 + 1:
sep_right_end = min(sep_right_end, pos)
return ((sep_left_end - sep_left_start) * (sep_right_end - sep_right_start)) % (
10 ** 9 + 7
)