Advertisement

Remove Invalid Parentheses - LeetCode 301 Solution

Remove Invalid Parentheses - Complete Solution Guide

Remove Invalid Parentheses is LeetCode problem 301, a Hard 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 that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid. Return a list of unique strings that are valid with the minimum number of removals . You may return the answer in any order . Example 1: Input: s = "()())()" Output: ["(())()","()()()"] Example 2: Input: s = "(a)())()" Output: ["(a())()","(a)()()"] Example 3: Input: s = ")(" Output: [""] Constraints: 1 <= s.length <= 25 s consists of lowercase English letters a

Detailed Explanation

The problem requires us to find all possible valid strings that can be obtained by removing the minimum number of invalid parentheses from a given string 's'. The input string consists of lowercase English letters and parentheses '(' and ')'. The output should be a list of unique valid strings. A valid string means that for every opening parenthesis '(', there is a corresponding closing parenthesis ')', and the parentheses are properly nested.

Solution Approach

The provided solution uses a Depth-First Search (DFS) approach (also known as backtracking). It calculates the minimum number of left and right parentheses that must be removed to make the string valid. This information is used to constrain the search space during the DFS. The DFS function explores the string character by character, considering two options for each parenthesis: remove it or keep it. The 'open_count' parameter tracks the number of unmatched open parentheses. If we encounter a closing parenthesis and there are no unmatched open parentheses, it must be removed. The base case for the recursion is when we have processed the entire string. The string is valid if all required parentheses have been removed (l_rem == 0 and r_rem == 0) and all open parentheses have been matched (open_count == 0).

Step-by-Step Algorithm

  1. Step 1: Calculate the minimum number of left parentheses (rem_l) and right parentheses (rem_r) that need to be removed to make the string valid. Iterate through the string, incrementing rem_l for each '(' and decrementing it for each ')' if rem_l > 0; otherwise, increment rem_r.
  2. Step 2: Implement a DFS function that takes the current index in the string, the number of left and right parentheses to remove (l_rem, r_rem), the number of open parentheses (open_count), and the current path (partial string).
  3. Step 3: The base case for the DFS is when the index reaches the end of the string. If l_rem, r_rem, and open_count are all zero, add the current path to the results set.
  4. Step 4: If l_rem + r_rem is greater than the remaining length of the string, then we have already pruned too few characters and must return, this speeds up runtime significantly.
  5. Step 5: For each character in the string, consider the following cases:
  6. a. If the character is '(', consider removing it (if l_rem > 0) or keeping it (increment open_count).
  7. b. If the character is ')', consider removing it (if r_rem > 0) or keeping it (if open_count > 0, decrement open_count).
  8. c. If the character is a letter, append it to the path and proceed to the next character.
  9. Step 6: After the DFS completes, convert the set of results to a list and return it.
  10. Step 7: Backtrack by removing the last character added to path after exploring both removal and keeping paths.

Key Insights

  • Insight 1: The core idea is to explore all possible combinations of removing parentheses using backtracking or BFS. Since we want to remove the *minimum* number of invalid parentheses, BFS can be efficient, especially in practice, as it explores solutions layer by layer.
  • Insight 2: We need to efficiently identify and remove the *minimum* number of parentheses. First, we can calculate the number of extra left and right parentheses that need to be removed. Then, during the search, we can prune branches that would lead to removing more parentheses than necessary. Early termination is crucial for performance.
  • Insight 3: To avoid duplicates in the results, use a `Set` data structure. This is essential since many different sequences of removals might lead to the same valid string.

Complexity Analysis

Time Complexity: O(2^N)

Space Complexity: O(N)

Topics

This problem involves: String, Backtracking, Breadth-First Search.

Companies

Asked at: Deliveroo, Google, Rubrik, Snowflake.