Check for a subsequence of size 1
for ( int j = 0 ; j < S . length ( ) ; j ++ ) { if ( S . charAt ( j ) != '2' && S . charAt ( j ) != '3' && S . charAt ( j ) != '5' && S . charAt ( j ) != '7' ) { System . out . println ( 1 ) ; flag = true ; break ; } }