[백준] 4949 - 균형잡힌 세상

zzenee·2022년 5월 23일
0

Algorithm&Coding-test

목록 보기
16/30
post-thumbnail

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

Problem

Code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.io.IOException;

class Main {
	public String VPS (String str) {
		char[] tmp = str.toCharArray();
		Stack<Character> stack = new Stack<>();
		for (int i=0; i<tmp.length; i++) {
            if (tmp[i] == '.') break;
            if ((tmp[i] == '(' || tmp[i] == '[')) stack.push(tmp[i]);
            else if (tmp[i] == ')') {
                if (!stack.isEmpty() && stack.peek() =='(') stack.pop();
                else stack.push(tmp[i]);
            }
            else if (tmp[i] == ']') {
                if (!stack.isEmpty() && stack.peek() =='[') stack.pop();
                else stack.push(tmp[i]);
            }
        }
        if (!stack.isEmpty()) return "no";
        else return "yes";
	}

  	public static void main(String[] args) throws IOException{
        Main ex = new Main();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String target = br.readLine();
            if (target.equals(".")) break;
            else System.out.println(ex.VPS(target));
        }
  	}
}

Result

profile
꾸준히

0개의 댓글