✔️Python, C++,Java|| Beginner level||Simple-Short-Solution||EasyToUnderstand✔️
Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)\nAny suggestions and improvements are always welcome.\n___\n__\nQ
***Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)\nAny suggestions and improvements are always welcome*.**\n___________________\n_________________\n***Q32. Longest Valid Parentheses***\n____________________________________________________________________________________________________________________\n____________________________________________________________________________________________________________________\n\u2705 **Python  Code** :\n```\nclass Solution:\n    def longestValidParentheses(self, s: str) -> int:\n        l,stack=0,[-1]\n        for i in range(len(s)):\n            if s[i]==\'(\':\n                stack.append(i)\n            else:\n                stack.pop()\n                if not stack:\n                    stack.append(i)\n                else:\n                    l=max(l,i-stack[-1])\n        return l;\n```\n____________________________________________________________________________________________________________________\n____________________________________________________________________________________________________________________\n\n\u2705 **Java Code** :\n```\nclass Solution {\n    public int longestValidParentheses(String s) {\n        Stack<Integer> stack = new Stack();\n        stack.push(-1);\n        int maxLen = 0;\n        for(int i = 0; i < s.length(); i++)\n        {\n            if(s.charAt(i) == \'(\')\n                stack.push(i);\n            else if(s.charAt(i) == \')\')\n            {    \n                stack.pop();\n                if(stack.empty())\n                    stack.push(i);\n                else\n                    maxLen = Math.max(maxLen, i - stack.peek());     \n            }\n        }\n        return maxLen;\n    }\n}\n```\n\n____________________________________________________________________________________________________________________\n____________________________________________________________________________________________________________________\n\u2705 **C++  Code** :\n```\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n    int n = s.length(),len =0,maxlen =0;\n    stack<int> st;\n    st.push(-1);\n    for(int i =0;i<n;i++)\n    {\n        if(s[i] == \'(\')\n            st.push(i);\n        if(s[i] == \')\')\n        {\n            st.pop();\n            if(st.empty())\n                st.push(i);\n            len = i - st.top();\n            maxlen = max(maxlen,len);\n        }\n    }\n    return maxlen;\n    }\n};\n```\n____________________________________________________________________________________________________________________\n____________________________________________________________________________________________________________________\nIf you like the solution, please upvote \uD83D\uDD3C\nFor any questions, or discussions, comment below. \uD83D\uDC47\uFE0F\n
['Stack', 'C', 'Python', 'Java']