Class Assignment Question

I am unable to put the condition for repeatition . Please help me with my code.
import java.util.*;
class Main{
public static Scanner scn = new Scanner(System.in);
public static void main(String [] args){

    // SUDOKU();
    // nqueen();
    // rat();
    // funky();
    // tricky();
    assign();
}

public static void assign(){
    int t = scn.nextInt();
    while(t-->0){
        int n = scn.nextInt();
        System.out.println(ca(n,"ab",""));
    }
}

public static int ca(int n_dig,String ques,String ans){
    if(ans.length()==n_dig){
        System.out.println(ans);
        return 1;
    }


    int count = 0;

    for(int i = 0; i< ques.length(); i++){
        char cc = ques.charAt(i);
        if(cc == 'b'){
        count += ca(n_dig,ques,ans + 'a');
        }else{
        count +=    ca(n_dig,ques,ans + 'a');
        count +=    ca(n_dig,ques,ans + 'b');
        }
    }
    return count;
}

}

@ap8730390,

Are you sure you have tagged the correct question? Are you attempting Class Assignment question or some other question? Because the code is very different.

Yeah, I am trying to attempt Class Assignment question using recursion

@ap8730390,

Suggested 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 .

For n=1 i.e. string of length 1
{a,b}=2
(First base case)

For n=2 i.e. string of length 2
{aa,ab,ba}=3 //b cannot be together
(second base case)

For n=3
{aaa,aba,baa,aab,bab}=5
fib[3]=fib[2]+fib[1]=3+2=5

For n=4
{aaaa,abaa,baaa,aaba,baba,aaab,abab,baab}=8
fib[4]=fib[3]+fiib[2]=5+3=8

so on…
Concluding: fib[n]=fib[n-1]+fib[n-1]

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.