Checking Balanced Brackets

Goutom Roy
1 min readJul 13, 2019

--

Python Implementation

In your development IDE, you might have noticed that it gives you error when you expression has missing enclosing brackets.How do they detect that closing bracket is missing ? Here I will write the solution as IDE does.

((10*5)+5 - missing brackets
{a+[c+(10*d)]} - ok
{x+[y+(z+d])} - missing brackets

Check the solution logic in HackerRank guide.I will just replicate this logic using stack(in python, it’s list) in code.

Output :

5
((10*5)+5
False
{a+[c+(10*d)]}
True
{x+[y+(z+d])}
False
{{[[(())]]}}
True

--

--

Goutom Roy

Engineer, son, brother, husband, friend, archaeology enthusiast, and history maniac.