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