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