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 string 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 |
Javascript
<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 approach function 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 code var 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)
Approach: Rotation
In this approach concatenate the input string with itself, and then check if the input string is a substring of the
concatenated string starting from any position. If we find such a position, it means that the input string can be obtained by
rotating some characters to the left or right.
Below is the implementation of the above approach:
C++
// C++ program of the above approach #include <iostream> #include <string> using namespace std; // Function to check if there exists a string which has left // shift and right shift both equal to the input string bool isLeftRightShiftEqual(string s) { int n = s.length(); // Concatenate the input string with itself string concatenated = s + s; for ( int i = 1; i < n; i++) { if (s == concatenated.substr(i, n)) { return true ; } } return false ; } // Driver Code int main() { string s = "papa" ; if (isLeftRightShiftEqual(s)) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; } |
Java
// Java code import java.io.*; public class LeftRightShiftEqual { // Function to check if there exists a string // which has left shift and right shift both equal to the input string public static boolean isLeftRightShiftEqual(String s) { int n = s.length(); // Concatenate the input string with itself String concatenated = s + s; for ( int i = 1 ; i < n; i++) { if (s.equals(concatenated.substring(i, i + n))) { return true ; } } return false ; } public static void main(String[] args) { String s = "papa" ; if (isLeftRightShiftEqual(s)) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } } // This code is contributed by guptapratik |
Python3
# Python code #Function to check if there exists a string which has left #shift and right shift both equal to the input string def isLeftRightShiftEqual(s): n = len (s) # Concatenate the input string with itself concatenated = s + s for i in range ( 1 , n): if s = = concatenated[i:i + n]: return True return False # Driver Code if __name__ = = "__main__" : s = "papa" if isLeftRightShiftEqual(s): print ( "Yes" ) else : print ( "No" ) # This code is contributed by guptapratik |
C#
using System; namespace LeftRightShiftEqual { class Program { // Function to check if there exists a string which has left // shift and right shift both equal to the input string static bool IsLeftRightShiftEqual( string s) { int n = s.Length; // Concatenate the input string with itself string concatenated = s + s; for ( int i = 1; i < n; i++) { if (s == concatenated.Substring(i, n)) { return true ; } } return false ; } // Driver Code static void Main( string [] args) { string s = "papa" ; if (IsLeftRightShiftEqual(s)) { Console.WriteLine( "Yes" ); } else { Console.WriteLine( "No" ); } } } } |
Javascript
// Function to check if there exists a string // which has left shift and right shift both equal to the input string function isLeftRightShiftEqual(s) { let n = s.length; // Concatenate the input string with itself let concatenated = s + s; for (let i = 1; i < n; i++) { if (s === concatenated.substring(i, i + n)) { return true ; } } return false ; } // Main function function main() { let s = "papa" ; if (isLeftRightShiftEqual(s)) { console.log( "Yes" ); } else { console.log( "No" ); } } // Call the main function main(); |
Yes
Time Complexity: O(n^2), where n is the length of the input string.
Auxiliary Space: O(n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!