Check if left and right shift of any string results into given string
Last Updated :
20 Mar, 2024
Given a non-empty 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++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG{
public static void check_string_exist(String S)
{
int size = S.length();
boolean check = true ;
for ( int i = 0 ; i < size; i++)
{
if (S.charAt(i) != S.charAt((i + 2 ) % size))
{
check = false ;
break ;
}
}
if (check)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
String S = "papa" ;
check_string_exist(S);
}
}
|
Python3
def check_string_exist(S):
size = len (S)
check = True
for i in range (size):
if S[i] ! = S[(i + 2 ) % size]:
check = False
break
if check :
print ( "Yes" )
else :
print ( "No" )
S = "papa"
check_string_exist(S)
|
C#
using System;
class GFG{
public static 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)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main(String[] args)
{
String S = "papa" ;
check_string_exist(S);
}
}
|
Javascript
<script>
function check_string_exist(S)
{
var size = S.length;
var check = true ;
for ( var i = 0; i < size; i++) {
if (S[i] != S[(i + 2) % size]) {
check = false ;
break ;
}
}
if (check)
document.write( "Yes" );
else
document.write( "No" );
}
var S = "papa" ;
check_string_exist(S);
</script>
|
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++
#include <iostream>
#include <string>
using namespace std;
bool isLeftRightShiftEqual(string s)
{
int n = s.length();
string concatenated = s + s;
for ( int i = 1; i < n; i++) {
if (s == concatenated.substr(i, n)) {
return true ;
}
}
return false ;
}
int main()
{
string s = "papa" ;
if (isLeftRightShiftEqual(s)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.io.*;
public class LeftRightShiftEqual {
public static boolean isLeftRightShiftEqual(String s) {
int n = s.length();
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" );
}
}
}
|
Python3
def isLeftRightShiftEqual(s):
n = len (s)
concatenated = s + s
for i in range ( 1 , n):
if s = = concatenated[i:i + n]:
return True
return False
if __name__ = = "__main__" :
s = "papa"
if isLeftRightShiftEqual(s):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
namespace LeftRightShiftEqual
{
class Program
{
static bool IsLeftRightShiftEqual( string s)
{
int n = s.Length;
string concatenated = s + s;
for ( int i = 1; i < n; i++)
{
if (s == concatenated.Substring(i, n))
{
return true ;
}
}
return false ;
}
static void Main( string [] args)
{
string s = "papa" ;
if (IsLeftRightShiftEqual(s))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
}
|
Javascript
function isLeftRightShiftEqual(s) {
let n = s.length;
let concatenated = s + s;
for (let i = 1; i < n; i++) {
if (s === concatenated.substring(i, i + n)) {
return true ;
}
}
return false ;
}
function main() {
let s = "papa" ;
if (isLeftRightShiftEqual(s)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
}
main();
|
Time Complexity: O(n^2), where n is the length of the input string.
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...