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.
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 <bits/stdc++.h> 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 |
Yes
Time Complexity: O(N) where N is the size of the string S.
Auxiliary Space Complexity: O(1)
Recommended Posts:
- Maximize count of 0s in left and 1s in right substring by splitting given Binary string
- Check if string is right to left diagonal or not
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Left Rotation and Right Rotation of a String
- Minimize characters to be changed to make the left and right rotation of a string same
- String formed with middle character of every right substring followed by left sequentially
- Queries for number of elements on right and left
- Find Partition Line such that sum of values on left and right is equal
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Longest subsequence of a number having same left and right rotation
- Check if a decreasing Array can be sorted using Triple cyclic shift
- Left-Right traversal of all the levels of Binary tree
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Check if any square (with one colored cell) can be divided into two equal parts
- Shift all prefixes by given lengths
- Maximum count of elements divisible on the left for any element
- String matching with * (that matches with any) in any of the two strings
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Check if characters of a given string can be used to form any N equal strings
- Find minimum shift for longest common prefix
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.


