VOOZH about

URL: https://www.geeksforgeeks.org/dsa/remove-invalid-parentheses/

⇱ Remove Invalid Parentheses - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Remove Invalid Parentheses

Last Updated : 4 May, 2026

Given a string str consisting only of lowercase letters and the characters '(' and ')'. Your task is to delete minimum parentheses so that the resulting string is balanced, i.e., every opening parenthesis has a matching closing parenthesis in the correct order, and no extra closing parentheses appear.

Return all distinct strings you can obtain by performing the minimum number of removals.

Examples:

Input: str = "()())()"
Output: "()()()" "(())()"
Explanation: We can remove the closing bracket at index 3 to obtain the balanced string "()()()". Similarly, we can remove the closing bracket at index 1 to obtain the balanced string "(())()".

Input: str = "(v)())()"
Output: "(v)()()" "(v())()"
Explanation: The given string is the modified version of first string containing a letter 'v' . As the letters do not differ the parentheses, the solution remains the same. 

Input: S = ")("
Output: ""

Using Breadth First Search (BFS) - O(2 ^ n) Time and O(2 ^ n) Space

The idea is to remove brackets one by one and check for balance. To remove brackets in a systematic way, we use BFS order. We remove one bracket at a time, check for balance, then remove more brackets and again check in BFS manner. We stop when we find a balanced string.

One important observation about this problem is, we can check the balancing by simply counting opening and closing brackets because we have only one type of brackets. We do not need to use stack to check.


Output
(())()
()()()

Using Depth First Search (DFS) - O(2 ^ n) Time and O(2 ^ n) Space

We first determine how many opening ( and closing ) parentheses are invalid and need to be removed. Then, use a recursive function to explore all ways of removing these parentheses while maintaining balance between open and close brackets. At each step, either skip a parenthesis (if it's invalid) or include it in the current string (if it helps form a valid expression), and continue building the string. Only strings that end with no unmatched parentheses are collected as valid results.

Step-by-step approach to process '())(':

  1. Count invalid parentheses in '())(' gives invalid open = 1 and invalid close = 1
  2. Start recursion at index 0 with open = 1, close = 1, pair = 0 and cur = ""
  3. At index 0 '(' choose skip making open = 0 or include making pair = 1 and cur = "("
  4. From skip path at index 1 ')' choose skip making close = 0, cannot include since pair = 0
  5. Continue to index 2 ')' cannot include and skip not allowed so this path is discarded
  6. From include path at index 0 go to index 1 ')' choose include making pair = 0 and cur = "()" or skip making close = 0
  7. From include case go to index 2 ')' cannot include since pair = 0 so skip making close = 0
  8. Move to index 3 '(' choose skip making open = 0 or include making pair = 1
  9. From skip case reach end with open = 0, close = 0, pair = 0 so add "()" to result
  10. From include case reach end with pair = 1 so discard

Final result is ["()"]


Output
(())()
()()()
Comment
Article Tags: