-
Check wheather the expression is valid or not Wipro previous year coding question
Sample input1:
“(())”
Sample output1:
Valid
Sample input2:
“()(”
Sample output2:
Invalid
Program:
n=input()
list1=list(n)
top=-1
list2=[]
for item in list1:
if item=='(':
top+=1
list2.append(item)
else:
list2.pop(top)
top-=1
if top==-1:
print("valid")
else:
print("invalid")
Post a Comment