Remove Invalid Parentheses

Given a string containing only parentheses and letters, your goal is to remove the minimum number of invalid parentheses to make the input string valid and return all possible valid strings.

Input:

The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains a single string ‘STR’ representing the parentheses and letters.

Output:

For each test case, return all possible unique valid strings obtained after removing the minimum number of parentheses. Each test case output should be printed in a separate line.

Example:

Input:
T = 2
STR = "()())()"
STR = "(a)())()"
Output:
["()()()", "(())()"]
["(a)()()", "(a())()"]

Constraints:

  • 1 <= T <= 5
  • 1 <= |STR| <= 50
  • Time Limit: 1 second

Note:

A string is valid if every left parenthesis has a corresponding right parenthesis in the same order. Example: 'STR' = "(())())" is not valid, but 'STR' = "(())()" is valid.
Be the first one to answer
Add answer anonymously...
MAQ Software Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter