Problem Solving/BOJ

[Silver 3] 10799번 쇠막대기

kmkunk 2022. 3. 22. 15:36
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Stack<Integer> stack = new Stack<Integer>();
        String s = br.readLine();
        String[] arr = s.split("");
        int count = 0;
        
        for(int i=0; i<arr.length; i++) {
            if(arr[i].equals("(")) {
                stack.push(i);
            } else {
                if(i-stack.peek()==1) {
                    stack.pop();
                    count += stack.size();
                } else {
                    stack.pop();
                    count++;
                }
            }
        }
        
        System.out.println(count);
    }
}