Source Code: class Solution: def maxScore(self, s: str) -> int: ''' In solving this problem, we can create two arrays , one array for keeping track of the number of zeroes on the left, and another array for keeping track of number of ones on the right. Then we will iterate through indexes and add the values of the leftzeroes and rightOnes together, and we'll eventually return the biggest possible combination that we can get! :D ''' max_score = 0 n = len(s) - 1 leftZeroes = [0] * n rightOnes = [0] * n for i in range(n): if s[i] == '0': if i == 0: leftZeroes[i] = 1 else: leftZeroes[i] = leftZeroes[i-1] + 1 else: if i >= 1: leftZeroes[i] = leftZeroes[i-1] for j in range(n,0,-1): if s[j] == '1': if j == n: rightOnes[j-1] = 1 else: rightOnes[j-1] = rightOnes[j] + 1 else: if j < n: rightOnes[j-1] = rightOnes[j] for i in range(n): max_score = max(max_score,leftZeroes[i] + rightOnes[i]) return max_score
Source Code:
class Solution:
def maxScore(self, s: str) -> int:
'''
In solving this problem, we can create two arrays , one array
for keeping track of the number of zeroes on the left,
and another array for keeping track of number of ones on the right.
Then we will iterate through indexes and add the values of
the leftzeroes and rightOnes together, and we'll eventually
return the biggest possible combination that we can get! :D
'''
max_score = 0
n = len(s) - 1
leftZeroes = [0] * n
rightOnes = [0] * n
for i in range(n):
if s[i] == '0':
if i == 0:
leftZeroes[i] = 1
else:
leftZeroes[i] = leftZeroes[i-1] + 1
else:
if i >= 1:
leftZeroes[i] = leftZeroes[i-1]
for j in range(n,0,-1):
if s[j] == '1':
if j == n:
rightOnes[j-1] = 1
else:
rightOnes[j-1] = rightOnes[j] + 1
else:
if j < n:
rightOnes[j-1] = rightOnes[j]
for i in range(n):
max_score = max(max_score,leftZeroes[i] + rightOnes[i])
return max_score