풀이과정
( -> push()
[ -> push()
) -> stack.peek()가 (인지 아닌지 또는 else _스택이 비어있는경우
] -> stack.peek()가 [인지 아닌지 또는 ''
. -> stack이 비었는지 남았는지
import java.io.*;
import java.util.*;
public class Problem_1_Anyeon00_try2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String str = br.readLine();
if(str.equals(".")){
break;
}
System.out.println(judge(str));
}
}
private static String judge(String str) {
for (int i = 0; i < str.length(); i++) {
char nowChar = str.charAt(i);
if (nowChar == '(' || nowChar == '[') {
stack.push(nowChar);
}
if (nowChar == ')' || nowChar == ']') {
if (stack.isEmpty()) {
return "no";
}
char peek = stack.peek();
if (nowChar == ')') {
if (peek != '(') {
return "no";
}
stack.pop();
} else if (nowChar == ']') {
if (peek != '[') {
return "no";
}
stack.pop();
}
}
}
if (stack.isEmpty()) {
return "yes";
}else {
return "no";
}
}
}