Check if left and right shift of any string results into given string - GeeksforGeeks

01 Sep, 2021
Given string S consisting of only lowercase English letters. The task is to find if there exists any string which has left shift and right shift both equal to string S. If there exists any string then print Yes, otherwise print No.
Examples: 
Input: S = “abcd” Output: No Explanation: There is no string which have left shift and right shift both equal to string “abcd”.
Input: papa Output: Yes Explanation: The left shift and right shift both of string “apap” equals to string “papa”.  
Approach: 
The main target is to check the left shift and right shift both of any string equals to given string or not.For that we just have to check every character of given string is equal to its next to next character or not (i.e. character at (i)th position must be equal to character at (i+2)th position ).If it’s true for every position on the given stringm then we can say there exist any string whose left shift and right shift equal to given string otherwise not.
The main target is to check the left shift and right shift both of any string equals to given string or not.
For that we just have to check every character of given string is equal to its next to next character or not (i.e. character at (i)th position must be equal to character at (i+2)th position ).
If it’s true for every position on the given stringm then we can say there exist any string whose left shift and right shift equal to given string otherwise not.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to check if left// and right shift of any string// results into the given string #include <bits/stdc++.h>using namespace std; // Function to check string exist// or not as per above approachvoid check_string_exist(string S){    int size = S.length();     bool check = true;     for (int i = 0; i < size; i++) {         // Check if any character        // at position i and i+2        // are not equal        // then string doesnot exist        if (S[i] != S[(i + 2) % size]) {            check = false;            break;        }    }     if (check)        cout << "Yes" << endl;    else        cout << "No" << endl;} // Driver codeint main(){    string S = "papa";     check_string_exist(S);     return 0;}
// Java program to check if left// and right shift of any string// results into the given stringclass GFG{     // Function to check string exist// or not as per above approachpublic static void check_string_exist(String S){    int size = S.length();    boolean check = true;         for(int i = 0; i < size; i++)    {               // 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");} // Driver Codepublic static void main(String[] args){    String S = "papa";         check_string_exist(S);}} // This code is contributed by divyeshrabadiya07
# Python3 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 approachdef check_string_exist(S):         size = len(S)    check = True         for i in range(size):                 # Check if any character        # at position i and i+2        # are not equal, then         # string doesnot exist        if S[i] != S[(i + 2) % size]:            check = False            break             if check :        print("Yes")    else:        print("No") # Driver CodeS = "papa" check_string_exist(S) # This code is contributed by divyeshrabadiya07
// C# program to check if left// and right shift of any string// results into the given stringusing System; class GFG{     // Function to check string exist// or not as per above approachpublic static void check_string_exist(String S){    int size = S.Length;    bool check = true;         for(int i = 0; i < size; i++)    {                // Check if any character       // at position i and i+2       // are not equal       // then string doesnot exist       if (S[i] != S[(i + 2) % size])       {           check = false;           break;       }    }    if (check)        Console.WriteLine("Yes");    else        Console.WriteLine("No");} // Driver Codepublic static void Main(String[] args){    String S = "papa";         check_string_exist(S);}} // This code is contributed by sapnasingh4991
<script> // Javascript 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 approachfunction check_string_exist(S){    var size = S.length;     var check = true;     for (var i = 0; i < size; i++) {         // Check if any character        // at position i and i+2        // are not equal        // then string doesnot exist        if (S[i] != S[(i + 2) % size]) {            check = false;            break;        }    }     if (check)        document.write( "Yes" );    else        document.write( "No" );} // Driver codevar S = "papa";check_string_exist(S); // This code is contributed by noob2000.</script>
Yes
 
Time Complexity: O(N) where N is the size of the string S.Auxiliary Space Complexity: O(1) 
divyeshrabadiya07
sapnasingh4991
noob2000
anikakapoor
Bit Magic
Competitive Programming
Pattern Searching
Strings
Strings
Bit Magic
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Program to find whether a given number is power of 2
Binary representation of a given number
Bits manipulation (Important tactics)
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Bits manipulation (Important tactics)
Top 10 Algorithms and Data Structures for Competitive Programming