수학 괄호 파싱

WooBuntu·2020년 8월 30일
0

JS 100제

목록 보기
17/34

수학 괄호 파싱

const e = "5 + 7) * (3 * 5)";

function isCorrectFormula(exp) {
  const stack = [];
  const arrOfInput = exp.split("").filter((char) => char != " ");
  for (const char of arrOfInput) {
    if (char == "(") {
      stack.push(0);
      continue;
    }
    if (char == ")") {
      if (stack.length == 0) {
        return false;
      }
      stack.pop();
    }
  }
  if (stack.length == 0) {
    return true;
  }
  return false;
}

0개의 댓글