Menu Close

Find the maximum score after splitting the string into two non-empty sub-strings.

Given a string s of zeros and ones, return the maximum score after splitting the string into two non-empty sub-strings (i.e. left sub-strings and right sub-strings). The score after splitting a string is the number of zeros in the left sub-strings plus the number of ones in the right sub-strings.
Input: s = ‘011101’
Output: 5

def splitin(x,y):
    sum=0
    for i in range(len(x)):
        if y == x[i]:
            sum+=1
    return sum
s=input("Enter the string of 0's and 1's : ")
L=[]
for i in range(1,len(s)):
    sum=0
    sum=splitin(s[:i],'0')+splitin(s[i:],'1')
    L.append(sum)
print(max(L))

Input_1:
Enter the string of 0’s and 1’s : 011101
Output:
5


Input_2:
Enter the string of 0’s and 1’s : 1111
Output:
3


Morae Q!