My code is not being accepted

My code is not being accepted

Hi @ayushijaan23
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.

1 Like

Thank you for helping me with the approach.
Can you suggest me changes in my code so that i can also implement this problem with dp…

@ayushijaan23 this is also a dp approach, https://ide.codingblocks.com/s/203328 have a look at the code.

1 Like

I have followed this dp approach

@ayushijaan23 it would be a different thing if you had come up with an approach and were unable to code it but it’s wrong to copy someone’s code without even understanding it! I suggest you either try to figure out what the above code is actually doing and then writing it from scratch, or just follow the other approach.

With all due respect, Ma’am, I don’t think that’s the case.The approach that i have followed is told to me in section :
:fire: Challenges - Recursion And Backtracking :Class Assignment Hint. Also it has been taught in dp section with ‘a’ and ‘b’ as ‘1’ and ‘0’.

@ayushijaan23 you are printing the value of “n” in the final answer, i think it is supposed to be the test case number, not n.