Skip to content

Latest commit

 

History

History
50 lines (38 loc) · 1.22 KB

_1249. Minimum Remove to Make Valid Parentheses.md

File metadata and controls

50 lines (38 loc) · 1.22 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 10, 2024

Last updated : July 01, 2024


Related Topics : String, Stack

Acceptance Rate : 70.03 %


Solutions

Python

class Solution:
    def minRemoveToMakeValid(self, s: str) -> str:
        output = list(s)
        opens = 0
        closes = 0
        for i in range(len(output)) :
            if output[i] == '(' :
                opens += 1
            elif output[i] == ')' :
                if opens > closes :
                    closes += 1
                else :
                    output[i] = ''
        
        curIndex = len(s) - 1
        while opens > closes and curIndex >= 0 :
            if output[curIndex] == '(' :
                opens -= 1
                output[curIndex] = ''
            curIndex -= 1
            
        return ''.join(output)