Check if any character at position i and i + 2 are not equal then string doesnot exist
if ( S . charAt ( i ) != S . charAt ( ( i + 2 ) % size ) ) { check = false ; break ; } } if ( check ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; }