[JavaScript] O(1) Space O(N) Time Intuitive Solution
Time: O(N)\nSpace: O(1)\njavascript\nvar longestValidParentheses = function(s) {\n    let open = 0, close = 0, maxLen = 0;\n\n    for(let c of s) {\n        if(
Time: `O(N)`\nSpace: `O(1)`\n```javascript\nvar longestValidParentheses = function(s) {\n    let open = 0, close = 0, maxLen = 0;\n\n    for(let c of s) {\n        if(c === \'(\') open++;\n        else close++;\n        if(open === close) maxLen = Math.max(maxLen, close*2);\n        else if(close > open) open = close = 0;\n    }\n\n    open = close = 0;\n    \n    for(let i = s.length-1; i >= 0; i--) {\n        if(s[i] === \')\') close++;\n        else open++;\n        if(open === close) maxLen = Math.max(maxLen, open*2);\n        else if(open > close) open = close = 0;\n    }\n    return maxLen;\n};\n```
['JavaScript']