Doubt regarding Finding CB numbers

This is my code :

import java.util.*;
public class Main {
public static void main(String args[]) {

	Scanner scan = new Scanner(System.in) ;
	String str = scan.next() ;
	ArrayList<Integer> al = new ArrayList<Integer>() ;

	for(int i = 0 ; i < str.length() ; i++ ){

		for(int j = i+1 ; j < str.length() ; j++ ){
			
			String s = str.substring(i,j) ;
			int num = Integer.parseInt(s) ;
			al.add(num) ;
		}
	}
	isPrime(al) ;
}

public static void isPrime(ArrayList<Integer> al){
	
	int count = 0 ;

	for(int i = 0 ; i < al.size() ; i++){
		
		boolean isPrime = true ;
		
		if(al.get(i) == 0 || al.get(i) == 1)
		  isPrime = false ;

		for(int j = 2 ; j <= Math.sqrt(al.get(i)) ; j++){
		
		   if(al.get(i) % j == 0){
			  isPrime = false ;
			  break ;
		   }
	}
	if(isPrime)
	  count ++ ;
}

System.out.print(count) ;

}
}

Majority of my test cases have been failed. Can you tell me where did I made mistake?

there are two major mistakes. please read the question again carefully.

  1. all the prime numbers are CB numbers but converse is not true. means CB numbers are superset. for eg 31*37 is a CB number but not a prime number.
  2. you are checking for every substring to be a CB number. that is fine. but once a substring is qualified as CB number, all the digits used in that CB string can’t be reused to form another CB number. you need to check on that.

Thanks

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.