JS No magic :( : approach #2 Beats 99% 2 approaches to choose from.  Feedback is welcome.
\n1.Sliding window \n-time O(N)\n-space O(N)\n\n\nvar longestValidParentheses = function(s) {\n    let longest = 0\n    let stack=[-1]\n    \n    for(let i=0;i<
\n1.Sliding window \n-time O(N)\n-space O(N)\n```\n\nvar longestValidParentheses = function(s) {\n    let longest = 0\n    let stack=[-1]\n    \n    for(let i=0;i<s.length;i++){\n      let char = s[i]\n      \n      if(char === \'(\'){\n        stack.push(i)\n        continue\n      }\n      \n      stack.pop()\n      if(!stack.length) stack.push(i)\n      else longest = Math.max((i - stack[stack.length-1]),longest)\n    }\n  \n    return longest\n};\n```\n\n2. Using left to right pass then right to left pass\n-time O(N)\n-space O(1)\n\n```\n\nfunction longestValidParentheses(s){\n  let longest = 0,leftCount = 0, rightCount = 0\n  \n  //left to right pass\n  for(let i=0; i < s.length;i++){\n    \n    if(s[i] === \'(\')leftCount++\n    else rightCount++\n    \n    if(leftCount === rightCount){\n      longest = Math.max(longest,leftCount+rightCount)\n    }else if(leftCount <rightCount){\n      leftCount=0\n      rightCount=0\n    }\n  }\n  \n  //right to left pass\n  leftCount = 0, rightCount = 0\n  for(let i=s.length-1; i >= 0;i--){\n\n    if(s[i] === \'(\')leftCount++\n    else rightCount++\n\n    if(leftCount === rightCount){\n      longest = Math.max(longest,leftCount+rightCount)\n    }else if(leftCount >rightCount){\n      leftCount=0\n      rightCount=0\n    }\n  }\n  return longest\n}\n\n```
['JavaScript']