Algorithm

[백준-9012번/Java] 괄호

1984 2022. 11. 17. 16:22

* stack 구조라고 생각하고 풀었다.

* count > 0 이면서 count ==0 으로 끝나야 함.

import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int N = Integer.parseInt(br.readLine());

		for (int i = 0; i < N; i++) {
			int count = 0;
			char[] str = br.readLine().toCharArray();

			for (char c : str) {
				if (count < 0) {
					break;
				} else if (c == '(') {
					count++;
				} else if (c == ')') {
					count--;
				}
			}
			if (count == 0) {
				System.out.println("YES");
			} else {
				System.out.println("NO");
			}
		}

	}

}
728x90