Check for a subsequence of size 2
if ( ! flag ) { loop : for ( int j = 0 ; j < S . length ( ) - 1 ; j ++ ) { for ( int j1 = j + 1 ; j1 < S . length ( ) ; j1 ++ ) { dummy = new StringBuilder ( Character . toString ( S . charAt ( j ) ) ) ; dummy . append ( S . charAt ( j1 ) ) ; if ( ! dummy . toString ( ) . equals ( "23" ) && ! dummy . toString ( ) . equals ( "37" ) && ! dummy . toString ( ) . equals ( "53" ) && ! dummy . toString ( ) . equals ( "73" ) ) { System . out . println ( 2 ) ; flag = true ; break loop ; } } } }