티스토리 뷰
https://www.acmicpc.net/problem/11899
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
System.out.println(getMinBracket(input));
}
static int getMinBracket(String str) {
Stack<Character> open = new Stack<>();
Stack<Character> close = new Stack<>();
int cnt = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == ')') {
if (!open.isEmpty()) {
open.pop();
} else {
close.push(c);
}
} else {
if (!close.isEmpty()) {
cnt += close.size();
close.clear();
}
open.push(c);
}
}
cnt += open.size() + close.size();
return cnt;
}
}