public class Solution {
    /**
     * 
     * @param s string字符串 
     * @return bool布尔型
     */
    public boolean isValid (String s) {
        // write code here
        Stack<Character> stack = new Stack();
        for(int i = 0; i< s.length();i++){
            char next = s.charAt(i);
            if(next == '(' || next == '{' || next == '['){
                stack.push(next);
            } else {
                if(stack.isEmpty()){
                    return false;
                }
                char pop = stack.pop();
                if((pop == '(' && next != ')') || (pop == '{' && next != '}') || (pop == '[' && next != ']')){
                    return false;
                }
            }
        }
        if(stack.isEmpty()){
            return true;
        }
        return false;
    }
}