Trains the technique from
LeetCode 301Remove Invalid ParenthesesThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A small template language allows lowercase letters and the two round brackets. A string counts as balanced when every ( is closed by a ) somewhere after it and every ) closes a ( somewhere before it. The empty string is balanced, and letters never affect whether a string is balanced.
The linter is handed s and must delete as few brackets as possible so that what remains is balanced. Letters are never deleted.
Return every distinct string that can be produced by deleting that smallest number of brackets. The order of the returned strings does not matter, but no string may appear twice.
Example 1
One closing bracket has nothing to close, so one deletion is enough. Dropping the bracket at position 3 leaves "a(bc)d" and dropping the one at position 5 leaves "a(b)cd"; both are balanced, so both are returned.
Example 2
The opening bracket is never closed, so it must go, and the letters stay put, leaving "fg".
Example 3
One of the two opening brackets has to go. Deleting either of them leaves the same string "(h)", and it is only listed once.
The values you return may be in any order.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def remove_invalid_parentheses(s: str) -> list[str]:public List<String> removeInvalidParentheses(String s)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.