Class Assignment

the function 1 gives TLE while function 2 gives wrong ans.
on dry runing both gives correct answer.

Any other approach to problem .

this question is a part of backtracting .

Hey @RULEREMPIRES
code is fine just a change
System.out.println("#" + (t+1) + " : " + Ndigit(num,0,""));
instead of System.out.println("#" + num + " : " + Ndigit(num,0,""));
Another approach
If observed carefully , we can identify that this is a problem for fibonacci series. This is because at the nth place , there are two possibilities.
Possibility 1 : We can choose to place the current character as β€˜a’. If so , then it doesn’t matter whether we placed β€˜a’ or β€˜b’ at the previous position. The total number of ways in this possibility would equal to f(n-1)
Possibility 2 : We can place the current character as β€˜b’. However we can only do it if the previous character was not β€˜b’ . Hence the total number of ways for this case must be f(n-2)
We add these two possibilities up and obtain the recursive relation
f(n) = f(n-1) + f(n-2)
This is clearly the recursive relation for Fibonacci Series .