Check if left and right shift of any string results into given string | Java program to check if left and right shift of any string results into the given string ; Function to check string exist or not as per above approach ; Check if any character at position i and i + 2 are not equal then string doesnot exist ; Driver Code
class GFG { public static void check_string_exist ( String S ) { int size = S . length ( ) ; boolean check = true ; for ( int i = 0 ; i < size ; i ++ ) { if ( S . charAt ( i ) != S . charAt ( ( i + 2 ) % size ) ) { check = false ; break ; } } if ( check ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " papa " ; check_string_exist ( S ) ; } }