백준 - 쇠막대기
내 풀이
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String brackets = br.readLine();
int ans = 0;
int stack = 0;
for (int i = 0; i < brackets.length(); i++) {
if (brackets.charAt(i) == '(') {
if (brackets.charAt(i + 1) == '(') {
stack += 1;
} else {
continue;
}
} else {
if (brackets.charAt(i - 1) == '(') {
ans += stack;
} else {
ans += 1;
stack -= 1;
}
}
}
System.out.println(ans);
}
}