AlgoDaily

Remove Invalid Parentheses

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Question

Given a string s that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid. Return all the possible results.

image

For example, consider the following string as input: s = "()())()" The correct solution should return ["(())()","()()()"], with both possibilities.

Note: this problem is similar to Minimum Parentheses Removal, with the difference being that we want to return all possible valid string results.

Constraints

  • 1 <= s.length <= 25
  • s consists of lowercase English letters and parentheses '(' and ')'.
  • There will be at most 20 parentheses in s.

CONTINUE WITH ALGODAILY PREMIUM

Keep going with the complete tutorial.

Unlock the remaining explanations and exercises, alongside the full learning library.

Explore Premium →
Open interactive view →