티스토리 뷰

https://www.acmicpc.net/problem/9012

 

9012번: 괄호

괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고

www.acmicpc.net

const fs = require('fs');
const [n, ...command] = fs.readFileSync("./dev/stdin").toString().trim().split("\n");

class Node{
    constructor(item){
      this.item = item;
      this.next = null;
    }
  }
  
  class Stack{
    constructor(){
      this.topOfStack = null;
      this.length = 0;
    }
  
    push(item){
      const node = new Node(item);
      if(this.topOfStack!=null){
        node.next = this.topOfStack; 
      }
        this.topOfStack = node;
        this.length+=1;
    }
  
    pop(){
      if(this.length==0)return -1;
      const popItem = this.topOfStack;
      this.topOfStack = popItem.next;
      this.length-=1;

      return popItem.item
    }
  
    size(){
      return this.length;
    }
  
    empty(){
      if(this.length==0) return 1;
      else return 0;
    }
  
    top(){
      if(this.length==0)return -1;
      return this.topOfStack.item; 
    }
  
  }


let answer = [];

command.forEach(v=>{
    let stack = new Stack(); 
    for(let i = 0; i<v.length; i++){
        if(stack.empty()==1){
            stack.push(v[i])
        }else if(stack.top()=='(' && v[i]==')'){
            stack.pop();
        }else{
            stack.push(v[i]);
        }
    }
    if(stack.empty()==1) answer.push('YES')
    else answer.push('NO')
})

console.log(answer.join('\n'));
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함