[백준 9012] 스택 - 괄호

김민지·2024년 1월 21일
0

냅다 시작 백준

목록 보기
110/118

✨ 문제 ✨



✨ 정답 ✨

const { json } = require("express/lib/response");
const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./예제.txt";
let input = fs.readFileSync(filePath).toString().trim().split('\n');

// const input = require('fs').readFileSync('/dev/stdin').toString().trim().split("\n")

const T = +input.shift();

const check = (theProblem) => {
  let answer = 'NO';
  let stack = [];
  for (let i = 0; i < theProblem.length; i++) {
    if (stack[stack.length - 1] === '(') {
      if (theProblem[i] === ')') {
        stack.pop();
      }else{
        stack.push(theProblem[i]);
      }
    }
    else if (stack[stack.length - 1]===')'){
      stack.push(theProblem[i]);
    }
    else{
      stack.push(theProblem[i])
    }
  }
  if (stack.length===0){
    answer='YES';
  }
  return answer;

}

for (let i = 0; i < T; i++) {
  let problem = input[i].trim().split('')
  console.log(check(problem))
}

🧵 참고한 정답지 🧵

💡💡 기억해야 할 점 💡💡

profile
이건 대체 어떻게 만든 거지?

0개의 댓글