Java |  Two Pointer | 100% faster | Constant Space
\nclass Solution {\n    public int longestValidParentheses(String s) {\n        int open=0;\n        int close=0;\n        int maxLen=0;\n        \n        for(
```\nclass Solution {\n    public int longestValidParentheses(String s) {\n        int open=0;\n        int close=0;\n        int maxLen=0;\n        \n        for(int i=0;i<s.length();i++){\n            char c=s.charAt(i);\n            \n            if(c==\'(\') open++;\n            else close++;\n            \n            if(open==close){\n                maxLen=Math.max(maxLen,open+close);\n            }\n            else if(close>open){\n                open=close=0;\n            }\n        }\n        \n        open=close=0;\n        \n        for(int i=s.length()-1;i>=0;i--){\n            char c=s.charAt(i);\n            \n            if(c==\'(\') open++;\n            else close++;\n            \n            if(open==close){\n                maxLen=Math.max(maxLen,open+close);\n            }\n            else if(close<open){\n                open=close=0;\n            }\n        }\n        return maxLen;\n    }\n}\n```
['Two Pointers', 'Java']