public int longestValidParentheses1(String s) {
        int max = 0;
        Stack<Integer> stack = new Stack<Integer>();
        for(int i = 0; i < s.length(); i++){
            Character ch = s.charAt(i);
            if(ch.equals(')') && !stack.isEmpty() && s.charAt(stack.peek()) == '('){
                stack.pop();
                if(stack.isEmpty()){
                    max = i + 1;
                }else{
                    max = Math.max(i - stack.peek(), max);
                }
            }else{
                stack.push(i);
            }
        }
        return max;
    }