제대로된 괄호 판단

괄호가 제대로 되어있는지 판단하기

js

const isCorrectParenthesis = (str) => {
  let balance = 0;

  for (const ch of str) {
    if (ch === '(') {
      balance++;
    } else {
      balance--;
    }
    if (balance < 0) return false;
  }

  return balance === 0;
};

isCorrectParenthesis('((()))'); // true
isCorrectParenthesis('(()))'); // false
isCorrectParenthesis(')))((('); // false