Check if left and right shift of any string results into given string | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check_string_exist ( string S ) { int size = S . length ( ) ; bool check = true ; for ( int i = 0 ; i < size ; i ++ ) { if ( S [ i ] != S [ ( i + 2 ) % size ] ) { check = false ; break ; } } if ( check ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { string S = " papa " ; check_string_exist ( S ) ; return 0 ; }