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

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”.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. The main target is to check the left shift and right shift both of any string equals to given string or not.
2. 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 ).
3. 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++

 `// C++ program to check if left ` `// and right shift of any string ` `// results into the given string ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to check string exist ` `// or not as per above approach ` `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) ` `        ``cout << ``"Yes"` `<< endl; ` `    ``else` `        ``cout << ``"No"` `<< endl; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string S = ``"papa"``; ` ` `  `    ``check_string_exist(S); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to check if left  ` `// and right shift of any string  ` `// results into the given string  ` `class` `GFG{ ` `     `  `// Function to check string exist  ` `// or not as per above approach  ` `public` `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 Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String S = ``"papa"``;  ` `     `  `    ``check_string_exist(S);  ` `} ` `} ` ` `  `// This code is contributed by divyeshrabadiya07 `

## Python3

 `# 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 approach  ` `def` `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 Code ` `S ``=` `"papa"` ` `  `check_string_exist(S) ` ` `  `# This code is contributed by divyeshrabadiya07 `

## C#

 `// C# program to check if left  ` `// and right shift of any string  ` `// results into the given string  ` `using` `System; ` ` `  `class` `GFG{ ` `     `  `// Function to check string exist  ` `// or not as per above approach  ` `public` `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 Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``String S = ``"papa"``;  ` `     `  `    ``check_string_exist(S);  ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

Output:

```Yes
```

Time Complexity: O(N) where N is the size of the string S.

Auxiliary Space Complexity: O(1)

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.