The following code is giving wrong answer for majority of test cases.
link-https://ide.codingblocks.com/s/108420
Wrong answer in tricky permutations problem
Hello @rishu7865,
Your code is printing wrong output because you have missed a very important requirement of the question:
The permutations should be printed in the lexicographical order.
Run your program for the following input to understand this better:
BCA
You can resolve this issue using two methods:
- Sort the input string before passing to the function.
- Create a vector and store all the permutations in that vector. Sort this vector and print all the strings in the vector.
But, the second case has comparatively higher memory requirement.
Hope, this would help.
Give a like, if you are satisfied.
Still after sorting the string it is giving wrong answer for majority of test cases.
I just checked the output,
It won’t give the correct output for the first point that i mentioned in my previous reply.
Reason:
after printing the first for permutations, for i=0, the j will become 2
so, it will swap A with C,
Thus, causing the wrong output.
Try out the second approach i have specified.
Hope, this would help.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.