I want to know how to further optimize the code. One way can be avoiding adding strings to vector but if i am directly giving the output then the order of answer is changed and all cases are failing.
Facing TLE in the last case
@HarmanHps you need not store the strings in a vector, simply print the strings in the base case.
And you are inserting strings in the front of vector each time…that takes O(size) time. Just think how much time it’d take if value of n is big.
I am guessing you did it to change the order of your output. There is a very simple way to do that, just reverse the order of your recursive calls. If that doesn’t strike your mind for some reason, inserting elements in the BACK of the vector and then traversing the vector in opposite direction is much more efficient (inserting in back only takes O(1) time if there is enough capacity)
If you do want to insert in front, deque is the data structure for that.
I have edited your code according to the simplest and most efficient method, just reversing the order of recursive calls, feel free to try out the other methods too. code: https://ide.codingblocks.com/s/291488
Thank You! That was really helpful.
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.