could you please help me optimize my solution
code: https://onlinegdb.com/MBy5tmONr
Code is giving TLE
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 .
refer this --> https://ide.codingblocks.com/s/619887
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.