Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags
more
Archives
Today
Total
관리 메뉴

main

[백준-9012번/Java] 괄호 본문

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

'Algorithm' 카테고리의 다른 글

[백준-3009번/Java] 네 번째 점  (0) 2022.11.17
[백준-1934번/Java] 최소공배수  (0) 2022.11.17
[백준-11047번/Java] 동전 0  (0) 2022.11.17
[백준-7576번/Java] 토마토  (0) 2022.11.17
[백준-11403번/Java] 경로찾기  (0) 2022.11.17
Comments