Advertisement

Minimum Remove to Make Valid Parentheses - LeetCode 1249 Solution

Minimum Remove to Make Valid Parentheses - Complete Solution Guide

Minimum Remove to Make Valid Parentheses is LeetCode problem 1249, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a string s of '(' , ')' and lowercase English characters. Your task is to remove the minimum number of parentheses ( '(' or ')' , in any positions ) so that the resulting parentheses string is valid and return any valid string. Formally, a parentheses string is valid if and only if: It is the empty string, contains only lowercase characters, or It can be written as AB ( A concatenated with B ), where A and B are valid strings, or It can be written as (A) , where A is a valid string. Exampl

Detailed Explanation

The problem asks us to take a string `s` that can contain open parentheses '(', close parentheses ')', and lowercase English letters. The goal is to remove the minimum number of parentheses to make the string a 'valid parentheses string'. A valid parentheses string is defined as either empty, contains only lowercase characters, can be written as AB (A concatenated with B) where A and B are valid strings, or can be written as (A), where A is a valid string. Essentially, every open parenthesis must have a corresponding closing parenthesis, and the closing parenthesis must come after the open one.

Solution Approach

The solution uses a stack to efficiently identify and remove the minimum number of invalid parentheses. It iterates through the string once. When an opening parenthesis is encountered, its index is pushed onto the stack. When a closing parenthesis is encountered, the stack is checked. If the stack is not empty, it means there is a matching open parenthesis, so we pop from the stack. If the stack is empty, the closing parenthesis is invalid and is marked for removal. After the first pass, any remaining open parentheses in the stack are also invalid and are marked for removal. Finally, a new string is constructed by excluding characters marked for removal.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty stack to store the indices of unmatched open parentheses.
  2. Step 2: Convert the input string to a list (or equivalent mutable structure) to allow character modification in place (Python), or create a string builder object (Java). C and C++ examples directly use vectors/arrays for the string representation.
  3. Step 3: Iterate through the string.
  4. Step 4: If the current character is an opening parenthesis '(', push its index onto the stack.
  5. Step 5: If the current character is a closing parenthesis ')', check if the stack is empty. If the stack is not empty, pop an index from the stack (indicating a match). If the stack is empty, mark the current character in the list/builder for removal.
  6. Step 6: After the loop, iterate through the remaining indices in the stack. For each index, mark the corresponding character in the list/builder for removal.
  7. Step 7: Construct a new string by iterating through the modified list/builder and appending only the characters that are not marked for removal.
  8. Step 8: Return the resulting valid string.

Key Insights

  • Insight 1: Use a stack to keep track of unmatched open parentheses. The index of each open parenthesis is pushed onto the stack.
  • Insight 2: When encountering a closing parenthesis, check if the stack is empty. If it's not empty, pop an open parenthesis from the stack, indicating a match. If it is empty, this closing parenthesis is unmatched and should be removed.
  • Insight 3: After iterating through the string, any remaining indices in the stack represent unmatched open parentheses that should also be removed.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: String, Stack.

Companies

Asked at: GE Digital, Netflix, Snap, Tencent.