I think this problem can be converted into fibonacci series
let f(n) gives no. of n digit numbers that can be formed
if 1st digit is a then we have to solve for f(n-1)
if 1st digit is b then 2nd digit must be ‘a’ , so we have to solve for f(n-2)
that means
f(n)=f(n-1)+f(n-2)
but it shows wrong answer on implementing this.
int f(int n)
{
if(n==1)
return 2;
if(n==2)
return 3;
if(n==0)
return 0;
int dp[n+1];
dp[0]=0;
dp[1]=2;
dp[2]=3;
for(int i=3;i<=n;i++)
{
dp[i]=dp[i-1]+dp[i-2];
}
return dp[n];
}