The score at any split point is the sum of zeros in the left substring plus the sum of ones in the right substring. The simplest approach is to try every valid split position and count directly. For each split, we scan the left portion to count zeros and the right portion to count ones, then track the maximum score found.
i from index 1 to n-1 (both substrings must be non-empty).0 to i-1).i to n-1).class Solution:
def maxScore(self, s: str) -> int:
n, res = len(s), 0
for i in range(1, n):
left_zero = 0
for j in range(i):
if s[j] == '0':
left_zero += 1
right_one = 0
for j in range(i, n):
if s[j] == '1':
right_one += 1
res = max(res, left_zero + right_one)
return resInstead of recounting zeros and ones for every split position, we can precompute cumulative counts. A prefix array stores the count of zeros up to each index, while a suffix array stores the count of ones from each index to the end. This way, evaluating any split becomes an O(1) lookup.
left_zero where left_zero[i] holds the count of zeros from index 0 to i.right_one where right_one[i] holds the count of ones from index i to n-1.i (from 1 to n-1):left_zero[i-1] + right_one[i].class Solution:
def maxScore(self, s: str) -> int:
n = len(s)
left_zero = [0] * n
right_one = [0] * n
if s[0] == '0':
left_zero[0] = 1
for i in range(1, n):
left_zero[i] = left_zero[i - 1]
if s[i] == '0':
left_zero[i] += 1
if s[n - 1] == '1':
right_one[n - 1] = 1
for i in range(n - 2, -1, -1):
right_one[i] = right_one[i + 1]
if s[i] == '1':
right_one[i] += 1
res = 0
for i in range(1, n):
res = max(res, left_zero[i - 1] + right_one[i])
return resWe can avoid storing full arrays by maintaining running counts. First, count all ones in the string. Then iterate through the string, incrementing zeros and decrementing ones as we move the split point. At each position, the current counts represent exactly what we need for the score calculation.
zero = 0 to track zeros seen so far.0 to n-2 (last valid split position):'0', increment zero.one (this one moves from right to left portion).zero + one.We can derive a single-pass solution using algebra. The score at position i equals left_zeros + right_ones. Since right_ones = total_ones - left_ones, the score becomes left_zeros + total_ones - left_ones, or equivalently total_ones + (left_zeros - left_ones). Since total_ones is constant, we only need to maximize (left_zeros - left_ones) while iterating, then add the total ones at the end.
zeros and ones counters.1 to n-1:(zeros - ones) before processing the current character.'0' or '1'.result + ones (where ones now contains the total count).class Solution:
def maxScore(self, s: str) -> int:
# res = Max of all (left_zeros + right_ones)
# res = Max of all (left_zeros + (total_ones - left_ones))
# res = total_ones (constant) + Max of all (left_zeros - left_ones)
zeros = 0
ones = 0
if s[0] == '0':
zeros += 1
else:
ones += 1
res = float('-inf')
for i in range(1, len(s)):
res = max(res, zeros - ones)
if s[i] == '0':
zeros += 1
else:
ones += 1
return res + ones