import java.util.*;


public class Solution {
    /**
     * 
     * @param s string字符串 
     * @return bool布尔型
     */
    public boolean isValid (String s) {
        Deque<Character> stack = new LinkedList<>();
        for(char c : s.toCharArray()){
            if(c == '(' || c == '[' || c == '{'){
                stack.push(c);
            }else {
                if(stack.isEmpty()) return false;
                switch(c){
                    case ')': if(stack.peek() != '(') return false;
                                stack.pop(); break;
                    case ']': if(stack.peek() != '[') return false;
                                stack.pop(); break;
                    case '}': if(stack.peek() != '{') return false;
                                stack.pop(); break;
                }
            }
        }
        return stack.isEmpty();
    }
}