백준 9012

Jb·2024년 3월 21일

백준

목록 보기
3/3

import java.io.*;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int testCase = Integer.parseInt(br.readLine());
        for (int i = 0; i < testCase; i++) {
            String input = br.readLine();
            if (isVps(input)) {
                System.out.println("YES");
            } else System.out.println("NO");
        }
    }

    public static boolean isVps(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == '(') {
                stack.push(c);
            } else if (c == ')') {
                if (stack.isEmpty()) {
                    return false;
                } else {
                    stack.pop();
                }
            }
        }
        return stack.isEmpty();
    }
}

생각의 흐름

1.참,거짓으로 vps를 판단하기 때문에 boolean메서드를 하나 만들어야한다.
2.stack(LIFO)구조로 (푸시하고 )이 있을떄 (이 있으면 팝을 하고 없으면 false로 반환

부족했던 점

1.순회라는 개념을 자꾸 생각을 안하려고한다.
2.toCharArray로 문자열을 문자배열 하나하나로 변환할수있다

0개의 댓글