Simple Java solution.
![enter image description here][1]\n\n\n  [1]: http://s11.postimg.org/9i4ju3imr/IMG_20150202_130621.jpg\n\n\n    public int longestValidParentheses(String s) {\
![enter image description here][1]\n\n\n  [1]: http://s11.postimg.org/9i4ju3imr/IMG_20150202_130621.jpg\n\n\n    public int longestValidParentheses(String s) {\n        char[] S = s.toCharArray();\n        int[] V = new int[S.length];\n        int open = 0;\n        int max = 0;\n        for (int i=0; i<S.length; i++) {\n        \tif (S[i] == '(') open++;\n        \tif (S[i] == ')' && open > 0) {\n        \t\tV[i] = 2 + V[i-1] + (i-2-V[i-1] > 0 ? V[i-2-V[i-1]] : 0);\n        \t\topen--;\n        \t}\n        \tif (V[i] > max) max = V[i];\n        }\n        return max;\n    }
[]