My simple Java solution
public int longestValidParentheses(String s) {\n        Stack<Integer> st = new Stack<Integer>();\n        int maxLen = 0;\n        int curLen = 0;\n        for
    public int longestValidParentheses(String s) {\n        Stack<Integer> st = new Stack<Integer>();\n        int maxLen = 0;\n        int curLen = 0;\n        for(int i = 0; i < s.length();i++) {\n            if(s.charAt(i) == '(') {\n                st.push(curLen);\n                curLen = 0;\n            } else if (s.charAt(i) == ')') {\n                if(st.isEmpty()) {\n                    curLen = 0;\n                } else {\n                    curLen = curLen + st.pop() + 2;\n                    if(curLen > maxLen) maxLen = curLen;\n                }\n            }\n        }\n        return maxLen;\n    }
[]