Search⌘ K
AI Features

Solution: Remove Invalid Parentheses

Explore a backtracking approach to remove invalid parentheses from a string. Understand how to count unmatched parentheses and use recursive exploration to generate all unique valid strings. This lesson helps you implement and analyze a solution to maintain balanced parentheses while minimizing removals.

Statement

You are given a string, s, that contains:

  • Lowercase English letters

  • Opening '(' and closing ')' parentheses

A string is considered valid if:

  1. All opening parentheses '(' are closed properly by a matching ')'.

  2. The parentheses are in the correct order and nesting.

  3. Letters can appear anywhere and do not affect validity.

Return all possible valid strings that can be formed by removing the minimum number of invalid parentheses. The answer must be a list of unique strings, in any order.

Constraints:

  • 1 ...