1249. Minimum Remove to Make Valid Parentheses

1249. Minimum Remove to Make Valid Parentheses

class Solution:
    def minRemoveToMakeValid(self, s: str) -> str:
        st=[]
        for i,v in enumerate(s):
            if v in '()':
                if len(st)==0:
                    st.append([v,i])
                else:
                    if st[-1][0]=='(' and  v==')':
                        st.pop()
                    else:
                        st.append([v,i])
        keys=[]
        while len(st)!=0:
            keys.append(st[-1][1])
            st.pop()
        keys=keys[::-1]
        s1=''
        j=0
        for i in range(len(s)):
            if j>=len(keys):
                s1+=s[i:]
                break
            elif i==keys[j]:
                j+=1
                continue
            else:
                s1+=s[i]
        return s1

            
            

你可能感兴趣的:(leetcode)