DEV Community

Discussion on: Checking if a string of parentheses are balanced in O(n) time and O(1) Space.

Collapse
 
erikpischel profile image
Erik Pischel

Just count I guess. +1 for every opening parentheses, -1 for every closeing one. Balance should always be >= 0, otherwise ")(" would be legal, and in the end, balance=0. Different counters for different types of parens.

If "([)]" is illegal then it's more complicated.