Hey brother @@swethag040 , I am preparing a video to show my solution. I am newbie to this TH-cam production thing so that is taking time to have the nerves to do it, but I promisse the solution is interestingly simpler. As soon as it is ready I'll let you know it here under this comment.
Yes you can. openingparenthesis = '[{(' closingparenthesis = ')}]' openingList = [] exp = '(){' pairs = {')':'(', '}':'{',']':'['} def checkParenthesis(exp): for i in exp: if i in openingparenthesis: openingList.append(i) elif i in closingparenthesis: if openingList==[]: return False top_char = openingList.pop() if pairs[i] != top_char: return False if openingList== []: return True else: return False print(checkParenthesis(exp))
Guys, I use the python strings approach. Very simple and effective.
Pls share the code
Hi @@swethag040 I'll try to find it at my computer at home and share it here.
Hey brother @@swethag040 , I am preparing a video to show my solution. I am newbie to this TH-cam production thing so that is taking time to have the nerves to do it, but I promisse the solution is interestingly simpler. As soon as it is ready I'll let you know it here under this comment.
Super amazing explanation
stack is not defined error
Sir but if won't work let's say ({) } it is actually balanced still it shows False
it's because we are popping { but char will )
Above expression is not balanced..
Type of bracket opened at last should be closed first
It will not work for nested parenthesis
Can I do it using dict??
Yes you can.
openingparenthesis = '[{('
closingparenthesis = ')}]'
openingList = []
exp = '(){'
pairs = {')':'(', '}':'{',']':'['}
def checkParenthesis(exp):
for i in exp:
if i in openingparenthesis:
openingList.append(i)
elif i in closingparenthesis:
if openingList==[]:
return False
top_char = openingList.pop()
if pairs[i] != top_char:
return False
if openingList== []:
return True
else:
return False
print(checkParenthesis(exp))
wow