import java.util.*;


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;
                }
            }
        }
	  	//最终全部对应上,栈中应该为空 
        return stack.isEmpty();
    }
}