Minimum Remove to Make Valid Parentheses ( LeetCode ) Problem Solution.

Minimum Remove to Make Valid Parentheses ( LeetCode ) Problem Solution.

Example 1:
Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would even be accepted.
Example 2:
Input: s = "a)b(c)d"
Output: "ab(c)d"
Example 3:
Input: s = "))(("
Output: ""
Explanation: An empty string is additionally valid.
Example 4:
Input: s = "(a(b(c)d)"
Output: "a(b(c)d)"

Constraints:
  • 1 <= s.length <= 10^5
  • s[i] is one amongst '(' , ')' and lowercase English letters.

Solution :-
class Solution:
    def minRemoveToMakeValid(self, s: str) -> str:
        st=[]
        res=[c for c in s]

        for i in range(len(s)):
            if(s[i]=='('):
                st.append(i)
            elif(s[i]==')'):
                if st:
                    st.pop()
                else:
                    res[i]=''

        for j in range(len(st)):
            res[st[-j]]=''

        return ''.join(res)


Runtime: 92 ms, faster than 93.99% of Python3 online submissions for Minimum Remove to form Valid Parentheses.
Memory Usage: 15.8 MB, but 100.00% of Python3 online submissions for Minimum Remove to form Valid Parentheses.