Contents

Baekjun No. 9012, parentheses (with.Java)

   Aug 9, 2025     3 min read

This article explores Baekjoon Problem 9012, parentheses (with.Java).

I’ll learn more by solving coding test problems, reflecting on previous problems, and exploring different solution methods.

Let’s first look at the problem.

Problem

A parenthesis string (PS) is a string composed solely of two parentheses, β€˜(β€˜ and β€˜)’.

A valid parenthesis string (VPS) is a string with correctly formed parentheses.

The string β€œ( )”, consisting of a pair of parentheses, is called a basic VPS.

If x is a VPS, then a new string β€œ(x)”, which encloses it in parentheses, is also a VPS.

Furthermore, a new string xy, which is the concatenation of two VPSs, x and y, is also a VPS.

For example, β€œ(())()” and β€œ((()))” are VPS strings, but β€œ(()(”, β€œ(())()))”, and β€œ(()” are all non-VPS strings.

You must determine whether the given parenthesized string is a VPS string and output YES or NO.

Input

The input data uses standard input.

The input consists of T test data.

The first line of the input contains an integer T, representing the number of input data.

Each test data line contains a parenthesized string.

The length of each parenthesized string must be greater than or equal to 2 and less than or equal to 50.

Output

The output uses standard output.

If the input parenthesized string is a valid parenthesized string (VPS), output β€œYES”; otherwise, output β€œNO”, one per line.

Problem Solution

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int T = Integer.valueOf(br.readLine());
        for(int i = 0; i < T; i++){
            int count = 0;
            char[] arr = br.readLine().toCharArray();
            for(char ch : arr){
                if(ch == '('){
                    count++;
                    if(count > arr.length){
                        break;
                    }
                }else{
                    count--;
                    if(count < 0){
                        break;
                    }
                }
            }
            if(count != 0){
                bw.write("NO");
                bw.newLine();
            }else{
                bw.write("YES");
                bw.newLine();
            }
        }
        br.close();
        bw.flush();
        bw.close();
    }
}

Solution Explanation

This code checks whether each string contains valid parentheses when given multiple parentheses.

The input and output are processed with BufferedReader and BufferedWriter for greater efficiency.

First, the number of test cases, T, is read from the first line.

For each test case, the parentheses string is read and converted to a character array.

Then, it iterates over each character and increments count if the opening parenthesis is β€˜(β€˜, and closes it if it is β€˜(β€˜). If it’s a parenthesis β€˜)’, count is decremented.

If count becomes negative during this process, it means there are more closing parentheses than opening parentheses, so the loop stops.

After the loop completes, if count is non-zero, print β€œNO”; otherwise, print β€œYES.”

This indicates whether each test case is a valid parenthesis or not.

Finally, after processing all input, output the results via BufferedWriter, emptying and closing the buffer.

This process efficiently verifies the validity of each parenthesis string.