문제

문제출처 : Baekjoon

img

풀이

import java.util.*;

public class Main {
    public String solution(String str){
        Stack<Character> stack = new Stack<>();
        String result = "yes";

        for(char x: str.toCharArray()){
            if(x == '(' || x== '['){
                stack.push(x);
            }else if(x == ')'){
                if(stack.empty() || stack.peek() != '('){
                    return "no";
                }else{
                    stack.pop();
                }
            }else if(x==']'){
                if(stack.empty() || stack.peek() != '['){
                     return "no";
                }else{
                    stack.pop();
                }
            }
        }

        if(!stack.isEmpty()){
            return "no";
        }
        
        return result;
    }

    public static void main(String[] args) {
        Main function = new Main();
        Scanner sc = new Scanner(System.in);
        while(true){
            String str = sc.nextLine();
            if(str.equals(".")){
                break;
            }
            System.out.println(function.solution(str));
        }
    }
}


회고

  1. yes, no를 대문자로 작성해서 계속 통과가 안됐다 바보…ㅋㅋㅋㅋ