Maximum Score After Splitting a String LeetCode solution in Python

1
# Runtime: 67 ms (Top 31.53%) | Memory: 13.9 MB (Top 60.59%)
2
class Solution:
3
def maxScore(self, s: str) -> int:
4
m0 = 0
5
m1 = 0
6
for i in s:
7
if i == "0":
8
m0 += 1
9
else:
10
m1 += 1
11
if m0 == 0 or m1 == 0:
12
return max(m0 - 1, m1 - 1)
13
l = len(s)
14
i = 0
15
max_ = 0
16
c0 = 0
17
c1 = m1
18
idx = -1
19
while i < l:
20
if s[i] == "0":
21
c0 += 1
22
else:
23
c1 -= 1
24
if max_ < c1 + c0:
25
max_ = c1 + c0
26
idx = i
27
max_ = max(max_, c1 + c0)
28
i += 1
29
if idx == l - 1:
30
return max_ - 1
31
return max_

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0