package leetcode;

/**
 * https://leetcode.com/problems/longest-valid-parentheses/
 */
public class Problem32 {
    public int longestValidParentheses(String s) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem32 prob = new Problem32();
        System.out.println(prob.longestValidParentheses("(()")); // 2
        System.out.println(prob.longestValidParentheses(")()())")); // 4
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -217,270 +217,1525 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem32 prob = new Problem32();%0A        System.out.println(prob.longestValidParentheses(%22(()%22)); // 2%0A        System.out.println(prob.longestValidParentheses(%22)()())%22)); // 4
+return longestValidParentheses(s, 0, 0, 0, 0, new HashMap%3C%3E()) * 2;%0A    %7D%0A%0A    private static int longestValidParentheses(String s, int idx, int begin, int end,%0A                                               int accu, Map%3CString, Integer%3E memo) %7B%0A        if (idx %3E= s.length()) %7B%0A            return accu;%0A        %7D%0A        String key = idx + %22%7C%22 + begin + %22%7C%22 + end;%0A        if (memo.containsKey(key)) %7B%0A            return memo.get(key);%0A        %7D%0A        int c = s.charAt(idx);%0A        int newBegin = begin;%0A        int newEnd = end;%0A        if (c == '(') %7B%0A            newBegin++;%0A        %7D else %7B // c == ')'%0A            newEnd++;%0A        %7D%0A        int a = longestValidParentheses(s, idx + 1, begin, end, accu, memo); // skip%0A        int b = 0;%0A        if (newBegin %3E= newEnd) %7B%0A            int newAccu = newEnd;%0A            b = longestValidParentheses(s, idx + 1, newBegin, newEnd, newAccu, memo);%0A        %7D%0A        int max = Math.max(a, b);%0A        memo.put(key, max);%0A        return max;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem32 prob = new Problem32();%0A        System.out.println(prob.longestValidParentheses(%22(()%22)); // 2%0A        System.out.println(prob.longestValidParentheses(%22)()())%22)); // 4%0A        System.out.println(prob.longestValidParentheses(%22(%22)); // 0%0A        System.out.println(prob.longestValidParentheses(%22()(())%22)); // 6%0A        System.out.println(prob.longestValidParentheses(%22((())))%22)); // 6%0A        System.out.println(prob.longestValidParentheses(%22()(()%22)); // 2
 %0A   

Update problem 32 (misunderstood the problem, doh!)