https://www.acmicpc.net/problem/9012
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import java.util.Scanner; public class Main { public static void checkStr(char[] c){ int cIdx = 0; int size = 0; while(cIdx < c.length) { if(c[cIdx] == '('){ size++; }else{ size--; } if(size < 0){ System.out.println("NO"); return; } cIdx++; } if(size == 0){ System.out.println("YES"); }else{ System.out.println("NO"); } } public static void main(String[] args){ int t; String str; Scanner sc = new Scanner(System.in); t = sc.nextInt(); sc.nextLine(); while( t > 0 ){ str = sc.nextLine(); char[] c = str.toCharArray(); if(c[0] == '(' && c[c.length - 1] == ')'){ checkStr(c); }else{ System.out.println("NO"); } t--; } } } | cs |
'알고리즘 > 백준 알고리즘' 카테고리의 다른 글
[code.plus 알고리즘 기초] no. 11727 (0) | 2019.01.17 |
---|---|
[code.plus 알고리즘 기초] no. 11726 (0) | 2019.01.16 |
[code.plus 알고리즘 기초]no. 1463 (0) | 2019.01.16 |
[code.plus 알고리즘 기초]no. 1158 (0) | 2019.01.14 |
[code.plus 알고리즘 기초]no. 10799 (0) | 2019.01.13 |