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.
AnswerBot
4mo

Given a string with parentheses and letters, remove minimum invalid parentheses to make it valid and return all possible valid strings.

  • Use BFS to explore all possible valid strings by removing parenth...read more

Help your peers!
Select
Add answer anonymously...

Top Software Engineer Interview Questions Asked at MAQ Software

Q. How does hashing help in identifying duplicate values?
Q. What is the difference between span and div tags?
Q. Given an array and a number, determine whether the number can be generated using...read more
Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits