# Balanced Parentheses checker in Python

I'd appreciate feedback on the code itself (style, performance optimization (both space and time)), and also on the algorithm (because I think this is typically implemented using a stack).

The assumption I'm making here is that there are only two constraints to having balanced parentheses; one is that there be the same number of opening and closing parentheses, the other one that there not be at any point more closing parentheses than opening ones.

With this in mind, I implement my balanced parentheses checker using a simple counter, which is incremented with each opening paren, and decremented with each closing paren.

The two "checks" within the function are that the counter never go negative (return False if it does at any point), and that at the end the counter be 0.

def paren_checker(string):
counter = 0
for c in string:
if c == '(':
counter += 1
elif c == ')':
counter -= 1
if counter < 0:
return False

if counter == 0:
return True
return False


Here is a somewhat improved version of the algorithm that allows for the three types:

def add_vectors(a, b):
for i, x in enumerate(b):
a[i] += x  # a is a list, so it supports item assignment

return a

def is_balanced(string):
#         (  [  {
counts = [0, 0, 0]

# dict of tuples to add based on char (like a switch statement)
d = {'(': ( 1, 0, 0), '[': ( 0, 1, 0), '{': ( 0, 0, 1),
')': (-1, 0, 0), ']': ( 0,-1, 0), '}': ( 0, 0,-1)}

for char in string:
try:
except KeyError:  # char is not in dict, so it isn't '(', '{', etc.
continue

if -1 in counts:  # close without open
return False

return counts == [0, 0, 0]  # will resolve to initial state if correct


Of course, this still doesn't cover the case of {[}] as is common in non-stack implementations.

I would replace your last lines with return counter == 0 as it is more compact and easier to read

• @pycoder not counter may be shorter but counter == 0 is more readable IMO – ovs Jan 20 '17 at 6:53

I checked your algorithm against a few examples on paper and it should actually work if I haven't missed anything. Nevertheless, it has a drawback. If you would like to introduce new types of parenthesis like braces {} or squared parenthesis [] it may not work properly and extending it may not be easy. When you use the stack data structure, it will be handled without huge problems.

• You're right; I actually just looked into extending my algorithm to other kinds of brackets and it does run into the problem of returning True for stuff like ( { ) }, which is obviously wrong. The stack makes the most sense I guess, but I'm thinking what I have above might be faster than a stack in case we're only dealing with the opening and closing of a single kind of symbol, since we only have to update a varialbe and not deal with a stack. – jeremy radcliff Jan 19 '17 at 22:17
• @jeremyradcliff extending is easy just add for lparen, rparen in ( ( '(', ')' ), ( '[', ']' )) and modify accordingly – Caridorc Jan 19 '17 at 22:28

Your code is good, but I have, in my opinon, a slightly more readable and potentially faster one: (This code will NOT check for the balenced open/closing part -- but it may still help others, or you could use it before checking with your idea)

def paren_checker(string):
if string.count("(") == string.count(")"):
return True
else:
return False


Or even better yet:

def paren_checker(string):
return string.count("(") == string.count(")")


I hope you find this useful (I had to do something similar before, but trust me, sometimes there are even more weird things that users could do!)

Even better yet: (Allows you to check ANY type of brackets, they don't even have to be brackets, they could be any type of start/close things you want)

def any_bracket_checker(string, bracket_type_start, bracket_type_end):
return string.count(bracket_type_start) == string.count(bracket_type_end)

print any_bracket_checker("(string)","(",")")

• This code does not work. You should re-read OP's assumptions: the string should contain equal amount of '(' and ')', that's one part; but you failed at considering the balanced part where there is more closing part than opening ones at some point, like in '()))((()'. – 301_Moved_Permanently Jan 20 '17 at 10:53
• @MathiasEttinger Oh yes. My bad. But i still believe some people may have a use for this code. Also, it can be used as a check before hand perhaps on a long string to save time (would it?). I will edit it my post to warn people of this caveat – John Hon Jan 20 '17 at 11:16