IT 정보/알고리즘(백준, BOJ)

[백준-BOJ] 9012

Dalyoung 2021. 5. 10. 22:26
728x90

www.acmicpc.net/problem/9012

 

9012번: 괄호

괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고

www.acmicpc.net

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
    public static void main(String[] args) throws IOException {
        Main m = new Main();
        m.doit();
    }
     
     
     
    public void doit() throws IOException{
        //System.setIn(new FileInputStream("input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int T = Integer.parseInt(br.readLine());
        for(int tc = 0; tc < T; tc++){
        	char[] p = br.readLine().toCharArray();
        	
        	int count = 0;
        	for(int i = 0; i < p.length; i++){
        		if(p[i] == '('){
        			count++;
        		}
        		if(p[i] == ')'){
        			count--;
        			
        			if(count < 0){
        				break;
        			}
        		}
        		
        	}
        	
        	if(count == 0){
        		System.out.println("YES");
        	}else{
        		System.out.println("NO");
        	}
        }
        br.close();
    }
}
728x90
반응형

'IT 정보 > 알고리즘(백준, BOJ)' 카테고리의 다른 글

[백준-BOJ] 9095  (0) 2021.05.10
[백준-BOJ] 9084  (0) 2021.05.10
[백준-BOJ] 8895  (0) 2021.04.07
[백준-BOJ] 7578  (0) 2021.04.07
[백준-BOJ] 6378  (0) 2021.04.07