General Parantheses Problem | Python | Interview Prep
Minimum Remove to Make Valid Parentheses\nhttps://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/\n\n\nclass Solution(object):\n    def minRemov
1249. Minimum Remove to Make Valid Parentheses\nhttps://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/\n\n```\nclass Solution(object):\n    def minRemoveToMakeValid(self, s):\n        """\n        :type s: str\n        :rtype: str\n        """\n        stack =[]\n        \n        s =list(s)\n        for i, char in enumerate(s):\n            if char == "(":\n                stack.append(i)\n            else:\n                if char == ")":\n                    if stack:\n                        stack.pop()\n                    else:\n                        s[i] = ""\n        \n        while stack:\n            s[stack.pop()] = "" \n        return "".join(s)\n```\n\n921. Minimum Add to Make Parentheses Valid\nhttps://leetcode.com/problems/minimum-add-to-make-parentheses-valid/\n\n```\nclass Solution(object):\n    def minAddToMakeValid(self, S):\n        """\n        :type S: str\n        :rtype: int\n        """\n        stack =[]\n        for i in S:\n            if i=="(":\n                stack.append(i)\n            \n            else:\n                if i == ")":\n                    \n                    if stack and stack[-1]=="(":\n                        stack.pop()\n                    else:\n                        stack.append(i)\n        \n        return len(stack)\n```\n\n20. Valid Parentheses\nhttps://leetcode.com/problems/valid-parentheses/\n\n```\nclass Solution(object):\n    def isValid(self, s):\n        """\n        :type s: str\n        :rtype: bool\n        """\n        dic ={"{":"}",\n              "[":"]",\n              "(":")"}\n                \n        stack = []\n        \n        for i in s:\n            if i in dic:\n                stack.append(i)\n                \n            # if there is input as "]", then we need to \n            # check the if the length of stack is empty or not.\n            elif len(stack) == 0 or dic[stack.pop()]!=i:\n                    return False\n        return len(stack)==0\n```\n\n32. Longest Valid Parentheses\nhttps://leetcode.com/problems/longest-valid-parentheses/\n```\nclass Solution(object):\n    def longestValidParentheses(self, s):\n        """\n        :type s: str\n        :rtype: int\n        """\n        stack =[]\n        \n        cur_long = 0\n        max_long = 0\n        \n        for i in s:\n            if i == "(":\n                stack.append(cur_long)\n                cur_long =0\n            else:\n                if stack:\n                    cur_long += stack.pop() + 2\n                    max_long = max(cur_long, max_long)\n                else:\n                    cur_long = 0\n        \n        return max_long\n```
['Python']