✅C# || ✅C++ ||🐍 Python || ✔Both DP and Stack Solution || 🚀 Beats 99%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n# If you find this helpful,Please Upvote \n```\npublic class Solution {\n    public int LongestValidParentheses(string s) {\n        Stack<int> index =new Stack<int>();\n            for(int i=0;i < s.Length;i++){\n                if (s[i] == \'(\'){\n                    index.Push(i);\n                }\n                else{\n                    if(index.Any() && s[index.Peek()] == \'(\'){\n                        index.Pop();\n                    }\n                    else{\n                        index.Push(i);\n                    }\n                }\n            }\n            if(!index.Any()){\n                return s.Length;\n            }\n            int length = s.Length, unwanted = 0;\n            int result = 0;\n            while(index.Any()){\n                unwanted = index.Peek();\n                index.Pop();\n                result = Math.Max(result,length - unwanted - 1);\n                length = unwanted;\n            }\n            result = Math.Max(result,length);\n            return result;\n    }\n}\n```\n# Python Solution  \n\n    def ValidParantheses(s):\n        length=len(s)\n        dp=[0]*length\n        count=0\n        maxLen=0\n        pos=0\n        for par in s:\n            if par == \'(\':\n                count += 1\n            elif par == \')\':\n                if count>0:\n                    count-=1\n                    #immediate parentheses like ()()\n                    dp[pos]=dp[pos-1]+2\n                    #outer parentheses (out of nested parentheses or non immmediate valid parentheses)\n                    if dp[pos] <= pos:\n                        dp[pos] = dp[pos]+dp[pos-dp[pos]]\n                \n            maxLen=max(maxLen,dp[pos])\n            pos += 1\n        return maxLen\n\n# Upvote \uD83E\uDD1E
['String', 'Dynamic Programming', 'Stack', 'Python', 'C#']