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...

MAQ Software Software Engineer interview questions & answers

A Software Engineer was asked 10mo agoQ. Given a singly linked list, reverse the nodes of the linked list k at a time and...read more
A Software Engineer was asked 11mo agoQ. How does hashing help in identifying duplicate values?
A Software Engineer was asked Q. Given an array, reverse the order of its elements.

Popular interview questions of Software Engineer

A Software Engineer was asked 10mo agoQ1. Given a singly linked list, reverse the nodes of the linked list k at a time and...read more
A Software Engineer was asked 11mo agoQ2. How does hashing help in identifying duplicate values?
A Software Engineer was asked Q3. Given an array, reverse the order of its elements.
MAQ Software 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