heres another approach. For given n sets, all permutations have a { in the beginning and } in the end. So, we need to permute the middle string with (n-1) sets. I have generated all the permutations of n sets i.e. say n ==3 then generate all permutations of (n-1==2 sets) {}{} string. Now before printing each permutation, run a check on it. This check will run through the middle permutation with sum initialized to 0 and then sum-- for every } encountered and sum++ for every { encountered for left to right traversal.
heres the code: output complies with catalan numbers. http://ideone.com/AgFeH i have a doubt regarding the complexity or lets say the efficiency. Any comments? On Jul 14, 9:10 am, shilpa gupta <shilpagupta...@gmail.com> wrote: > 1. Write the code for this > > input: 1 > output: > {} > input: 2 > output: > {}{} > {{}} > input: 3 > output: > {}{}{} > {{}}{} > {}{{}} -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.