Skip to content

Commit a6e05ef

Browse files
authored
Create number-of-ways-to-split-a-string.py
1 parent cc9bc5e commit a6e05ef

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def numWays(self, s):
6+
"""
7+
:type s: str
8+
:rtype: int
9+
"""
10+
MOD = 10**9+7
11+
12+
ones = s.count('1')
13+
if ones % 3:
14+
return 0
15+
ones //= 3
16+
if ones == 0:
17+
return (len(s)-1)*(len(s)-2)//2 % MOD
18+
count = left = right = 0
19+
for c in s:
20+
if c == '1':
21+
count += 1
22+
if count == ones:
23+
left += 1
24+
elif count == 2*ones:
25+
right += 1
26+
return left*right % MOD

0 commit comments

Comments
 (0)