Baekjoon - 4949

Tadap·2023년 9월 15일
0

Baekjoon

목록 보기
17/94
post-custom-banner

문제
Solved.ac class2++

1차시도

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		HashMap<String, String> map = new HashMap<>();
		map.put("(", ")");
		map.put("[", "]");
		String line = br.readLine();

		while (!line.equals(".")) {
			Stack<String> stack = new Stack<>();


			String[] split = line.split("");
			for (String s : split) {
				if (s.equals("(") || s.equals("[")) {
					stack.push(s);
				} else if (s.equals(")") || s.equals("]")) {
					if (stack.isEmpty()) {
						stack.push("1");
						break;
					}
					if (map.get(stack.peek()).equals(s)) {
						stack.pop();
					} else {
						break;
					}
				}
			}
			if (stack.isEmpty()) {
				sb.append("yes").append("\n");
			} else {
				sb.append("no").append("\n");
			}
			line = br.readLine();
		}
		System.out.println(sb);
	}
}

성공

post-custom-banner

0개의 댓글