X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Remove Invalid Parentheses
Medium Accuracy: 19.85% Submissions: 811 Points: 4

You are given a string S that contains parentheses and letters. You have to remove the minimum number of invalid parentheses to make the input string valid.

Return all the possible results. You may return the answer in any order.

Example 1:

Input: S = "()())()"
Output: ["(())()","()()()"]

Example 2:

Input: S = "(a)())()"
Output: ["(a())()","(a)()()"]

Example 3:

Input: S = ")("
Output: [""]

Your Task:  
You don't need to read input or print anything. Your task is to complete the function removeInvalidParentheses() which takes the string S as an input parameter and returns an array of strings representing all the valid parentheses that we can form by removing the minimum number of characters from the string.

 

Expected Time Complexity: O(2|S|)
Expected Auxiliary Space: O(1)

Constraints:
1 <= |S| <= 20
S consists of lowercase English letters and parentheses '(' and ')'

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Remove Invalid Parentheses

Output Window