659. Remove Invalid Parentheses

HardStackStackBFSString

Given a string s of parentheses and lowercase letters, remove the minimum number of parentheses to make it valid, and return all distinct valid results in ascending (lexicographic) order. The input is JSON {s}.

Input: JSON {s}.

Output: Array — the distinct valid strings, sorted ascending.

Examples

Example 1
Input: {"s":"()())()"}
Output: ["(())()","()()()"]
Explanation: Two minimal removals.
Example 2
Input: {"s":"(a)())()"}
Output: ["(a())()","(a)()()"]
Explanation: Letters are preserved.
Example 3
Input: {"s":")("}
Output: [""]
Explanation: Everything removable.

Constraints

Asked by

MetaOracleMicrosoftGoogleAmazonBloomberg
Solve this problem in the editor →