BINARYST-editorial

PROBLEM LINK:

Practice
Contest

Author: Satwato
Editorialist: Satwato

DIFFICULTY:

SIMPLE-EASY

PROBLEM:

Given a string of arbitrary characters which can contain numbers, write a program to print the longest binary sub-string having no leading zero(s), no space(s), and the maximum numeric value.

QUICK EXPLANATION:

Problem can be solved either with regex or in O(N) complexity by iterating over a string once. While iterating letter by letter we can filter out the binary numbers.

EXPLANATION:

We take an empty string, eg: t=“”
And we take an empty array, eg: store
For char in string:
if char ==‘1’ or char == ‘0’ then we add to empty string t
else: we check if string t has length > 0 , then append t to array store and reinitialize t= “”

print( max( store ))

SOLUTIONS:

Setter's Solution

T=int(input())

while(T>0):
stri=input()
store=[]
temp=‘’
for i in range(len(stri)):
if stri[i]==‘1’ or stri[i]==‘0’:
temp+=stri[i]

  else:
     if len(temp)>0:	
        store.append(int(temp))
        temp=''

if len(temp)>0: #after iteration, if any string is stored in temp, append it
store.append(int(temp))

if len(store):
print(str(max(store)))
else:
print(“IMPOSSIBLE”)
T-=1

Editorialist's Solution

import re
T=int(input())
while(T>0):
stri=input()
store=re.findall(r"[0-1]+",stri)
store=[int(i) for i in store]
if len(store):
print(str(max(store)))
else:
print(“IMPOSSIBLE”)
T-=1