Parenthesis Problems
checking Parenthesis Validity
caution
- starting the
balance
with0
- for every
(
++balance - for every
)
--balance - To Note here: whenever my balance has more
)
than(
, then I would have--balance
more times, Hence, thebalance < 0
, thusfalse
- Also to note at the very end I should get equal no. of
++balance
&--balance
, Hence MUST havebalance == 0
elsefalse
int balance = 0;
for (auto &el : s)
{
if (el == '(')
++balance;
else // (el == ')')
--balance;
if (balance < 0)
return false;
}
return (balance == 0);