Update appNew update is available. Click here to update.

Generate All Valid Strings

Posted: 16 Feb, 2021
Difficulty: Easy


Try Problem

Ninja has been given a string ‘STR’ containing ‘(‘ and ‘)’. ‘STR’ may also contain lower case alphabets. In order to make ‘STR’ valid Ninja can remove either ‘)’ or ‘(’ from ‘STR’.

Here are some examples of valid ‘STR’ “((a))”, “(g)()”, “(())()”, "" (an empty string is also a valid string).

Ninja wants to know all the possible valid strings that can be formed from ‘STR’ by minimum possible removals (possibly zero).

Can you help Ninja to generate all valid strings from ‘STR’ by minimum removals?

For Example :
For the given ‘STR’ =  “()(()”, we can form a valid string “()()” by removing ‘(‘ at index 2 or 3.
Input Format :
The first line of input contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first and the only line of each test case contains a string ‘STR’
Output Format :
For each test case, print all possible unique valid strings that can be formed from ‘STR’ in a separate line.

Print the output of each test case in a separate line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints :
1 <= ‘T’ <= 100
‘STR[i]’ = ‘(‘, ‘)’ or Lower case english alphabet
1 <= |STR| <= 2000

Where |STR| denotes the length of the given string.

Time Limit: 1 sec