Please review my code only TestCase 1 pass rest fail

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String strng = sc.next();

	CBnumber(strng);

}

static void CBnumber(String st) {

	boolean flag = false;
	int len = st.length();

	int count = 0;
	for (int i = 0; i <=len; i++) {
		for(int j = i+1 ; j<=len ; j ++) {

			String stn = st.substring(i,j);

			int a = Integer.valueOf(stn);
			if (stn.equals("1") || stn.equals("0")) {

				flag = false;
			}

			else if ((stn.equals("2")) || (stn.equals("3")) || stn.equals("5") || stn.equals("7")
					|| stn.equals("11") || stn.equals("13") || stn.equals("17") || stn.equals("19")
					|| stn.equals("23") || stn.equals("29")) {

				flag = true;
				

			}

			else if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0 || a % 7 == 0 || a % 11 == 0 || a % 13 == 0
					|| a % 17 == 0 || a % 19 == 0 || a % 23 == 0 || a % 29 == 0) {
				flag = false;

			}
		else {
				
				flag = true;
			
		}
			
			if (flag == true) {
				count++;
				System.out.println(stn);
			}
			
			
		}

	
	}
	
	System.out.println(count);
}

}

hi @k.bhupenderagile
you have to find distinct cb numbers that means not the repeated ones.but your code will also calculate the repeated ones as well.

all three Count are distinct in my case

@k.bhupenderagile
you are not taking care of this case.
CB number once detected should not be sub-string or super-string of any other CB number.
Ex- In 4991, both 499 and 991 are CB numbers but you can choose either 499 or 991, not both.

how to handle that case

@k.bhupenderagile
you can first the check all the cb number with length 1 then marked all those number as used (you
can do this will the help of boolean array) then search for all the cb numbers of length 2 and mark them as well. if you find cb number with length 2 such that one of it element is marked then you cannot take that cb numbers.

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.