bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 301. Remove Invalid Parentheses

301. Remove Invalid Parentheses

Leetcode Solutions

Backtracking with Pruning

  1. Calculate the number of misplaced left and right parentheses (left_rem and right_rem).
  2. Use a helper function to perform backtracking, starting with the first character of the string.
  3. If the current character is not a parenthesis, add it to the current expression and continue.
  4. If the current character is a parenthesis, consider two cases: including it or excluding it from the current expression.
  5. When excluding a parenthesis, decrement the corresponding left_rem or right_rem.
  6. Only include a right parenthesis if it does not lead to an invalid expression (i.e., right_count < left_count).
  7. If the end of the string is reached and left_rem and right_rem are both zero, add the current expression to the result set if it's not already present.
  8. After exploring all possibilities, return the contents of the result set as a list.
UML Thumbnail

Breadth-First Search (BFS)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...