728x90
문제
풀이
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Stack;
public class BalanceWorld {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String s;
while(true) {
s = br.readLine();
if(s.equals(".")) {
break;
}
sb.append(solve(s)).append('\n');
}
System.out.println(sb);
}
public static String solve(String s) {
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == '(' || c == '[') {
stack.push(c);
} else if(c == ')') {
if(stack.empty() || stack.peek() != '(') {
return "no";
} else {
stack.pop();
}
} else if(c == ']') {
if(stack.empty() || stack.peek() != '[') {
return "no";
} else {
stack.pop();
}
}
}
if(stack.empty()) {
return "yes";
} else {
return "no";
}
}
}
728x90
'백준' 카테고리의 다른 글
백준 18258 - Java (1) | 2024.09.05 |
---|---|
백준 12789 - Java (0) | 2024.09.04 |
백준 10773 - Java (0) | 2024.09.04 |
백준 28278 - Java (0) | 2024.09.04 |
백준 17103 - Java (0) | 2024.09.04 |