Remove Invalid Parentheses
Given a string s containing letters and parentheses, remove the minimum number of invalid parentheses so that the resulting string is valid. Return all unique valid strings achievable with the minimum removals.
Open official problem prompt ↗Find all shortest-edit-distance valid strings reachable by deleting only parentheses, deleting as few as possible.
Like fixing an unbalanced set of brackets in a document by erasing the fewest brackets; you first tally how many extras exist, then try each way of erasing exactly that many and keep the balanced results.
- Input
- s = "()())"
- Output
- ["(())","()()"]
- Why
- There is one extra ')'. Removing exactly one ')' yields the valid strings (()) and ()(); both use the minimum of one removal.
1 <= s.length <= 25s consists of lowercase letters and the characters '(' and ')'There are at most 20 parentheses in s