Maximum Length - 0s or 1s
Maximum Length - 0s or 1s - CTS GenCNext
Problem:
The program must accept a string S containing 0s and 1s as the input. The program must print the length of the longest substring in S consists of the maximum number of either 0s or 1s that appear consecutively. (However, the substring cannot occur at the beginning or end of the string). If there is no such substring, the program must print -1 as the output.
Boundary Condition(s):
1 <= Length of S <= 100
Input Format:
The first line contains S.
Output Format:
The first line contains the length of the longest substring in S or -1 as per the given conditions.
Example Input/Output 1:
Input: 10101000
Output:
1
Explanation:
Here the given string is 10101000. The length of the longest substring with consecutive 0s or 1s (expect the substring at the beginning and end of the string S) is 1. Hence the output is 1.
Example Input/Output 2:
Input:
010000001
Output:
6
Example Input/Output 3:
Input:
111100000
Output:
-1
Program:
s = input().strip()
a = s[0]
m = []
for i in range(1,len(s)):
if a[-1] == s[i]:
a += s[i]
else:
m.append(len(a))
a = s[i]
if a != "":
m.append(len(a))
try:
print(max(m[1:-1]))
except:
print(-1)
Comments
Post a Comment