Stack Solution - Video Explanation
Solution Video\n\nhttps://youtu.be/HQJL2_73xmA\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my channel! \u2B50\uFE0F\u2B50\uFE0F\n\n\u25A0 Subs
# Solution Video\n\nhttps://youtu.be/HQJL2_73xmA\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my channel! \u2B50\uFE0F\u2B50\uFE0F\n\n**\u25A0 Subscribe URL**\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\nSubscribers: 6,001\nThank you for your support!\n\n---\n\nhttps://youtu.be/bU_dXCOWHls\n\n---\n\n```python []\nclass Solution:\n    def longestValidParentheses(self, s: str) -> int:\n        stack = [-1]\n        max_len = 0\n\n        for i in range(len(s)):\n            if s[i] == "(":\n                stack.append(i)\n            else:\n                stack.pop()\n                if len(stack) == 0:\n                    stack.append(i)\n                else:\n                    max_len = max(max_len, i - stack[-1])\n        \n        return max_len\n```\n```javascript []\nvar longestValidParentheses = function(s) {\n    const stack = [-1];\n    let max_len = 0;\n\n    for (let i = 0; i < s.length; i++) {\n        if (s[i] === "(") {\n            stack.push(i);\n        } else {\n            stack.pop();\n            if (stack.length === 0) {\n                stack.push(i);\n            } else {\n                max_len = Math.max(max_len, i - stack[stack.length - 1]);\n            }\n        }\n    }\n\n    return max_len;\n};\n```\n```java []\nclass Solution {\n    public int longestValidParentheses(String s) {\n        Stack<Integer> stack = new Stack<>();\n        stack.push(-1);\n        int max_len = 0;\n\n        for (int i = 0; i < s.length(); i++) {\n            if (s.charAt(i) == \'(\') {\n                stack.push(i);\n            } else {\n                stack.pop();\n                if (stack.isEmpty()) {\n                    stack.push(i);\n                } else {\n                    max_len = Math.max(max_len, i - stack.peek());\n                }\n            }\n        }\n\n        return max_len;        \n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        stack<int> st;\n        st.push(-1);\n        int max_len = 0;\n\n        for (int i = 0; i < s.length(); i++) {\n            if (s[i] == \'(\') {\n                st.push(i);\n            } else {\n                st.pop();\n                if (st.empty()) {\n                    st.push(i);\n                } else {\n                    max_len = max(max_len, i - st.top());\n                }\n            }\n        }\n\n        return max_len;        \n    }\n};\n```\n\n---\n\nThank you for reading my post.\n\n##### \u2B50\uFE0F Subscribe URL\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\n##### \u2B50\uFE0F Twitter\nhttps://twitter.com/CodingNinjaAZ\n\n##### \u2B50\uFE0F My recent video\n#20 Valid Parentheses\n\nhttps://youtu.be/-CGiyb1Loyc\n
['C++', 'Java', 'Python3', 'JavaScript']