Generate Parentheses Problem

The output strings should be printed in the sorted order considering ‘(’ has a higher value than ‘)’.
plz explain with example…
As i have solve this problem but without sort …
so,getting wrong answer

Hi Nargis, consider the sample testcase itself. Now the 2 possible strings that can be made are

( ) ( ) & ( ( ) )

Let’s treat them as numbers. Since it is given that ‘(’ has higher value than ‘)’, so let:
‘(’ = 2
‘)’ = 1

Now this means:

( ) ( ) = 2121
( ( ) ) = 2211

since 2121 < 2211 thus 2121 gets printed first( sorted in increasing order)

thus output shall be

2121
2211

//which is

( ) ( )
( ( ) )

Now it’s a fact that ‘)’ > ‘(’. :

bool res = ‘)’ > ‘(’ ;
cout << res ;

// Output
True

So after you have generated all possible strings just sort them in decreasing order and print :slight_smile: .

Hey Nargis, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.

thank u
I got it …:grinning::blush:

1 Like