[백준] JAVA - 괄호 (9012)
문제
9012번: 괄호
괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고
www.acmicpc.net
풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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());
String[] input = new String[n];
int i;
for (i = 0; i < n; i++) {
input[i] = br.readLine();
}
for(int j=0;j<input.length;j++) {
int count =0;
String list[] = input[j].split("");
for(int k=0;k<input[j].length();k++){
if(count<0){
break;
}
if(list[k].equals("(")) {
count++;
} else {
count--;
}
}
if(count<0 || count != 0){
System.out.println("NO");
} else {
System.out.println("YES");
}
}
}
}