Write a program in C++ to check if a string can be obtained by rotating another string by two places


Suppose we’ve given two strings ‘a’ and ‘b’, the task is to find whether we can obtain the string ‘b’ by rotating string ‘a’ exactly by two places in an anticlockwise or clockwise direction. For example,

Input-1

a = google
b = legoog

Output

True

Explanation − String ‘google’ can be rotated in an anticlockwise direction by two places, which results in the string ‘legoog’. Thus, we return True.

Input-2

a = tuorialst
b = tutorials

Output

False

Explanation- String ‘tuorialst’ cannot be rotated by two places in any direction to get another string ‘tutorials’. Thus, we return False.

The approach used to solve this problem

For the given two strings, we have two cases in this approach −

  • For Anticlockwise rotation

  • For Clockwise rotation

First, if the length of both strings is different, then return false. Otherwise, if the length of both strings is less than or equal to ‘2’, then we will return True.

In other cases, we will check if the substring of string ‘b’ by rotating two places in an anticlockwise direction is equal to string ‘a’, then we will return True. Otherwise, False.

Similarly, if by rotating string ‘b’ in a clockwise direction by two places, it becomes equal to the string ‘a’, then return True, otherwise, we will return false.

  • Take two Input strings ‘a’ and ‘b’

  • A Boolean function checkRotated( string a, string b) takes two strings ‘a’ and ‘b’, and returns if they are equal by rotating string ‘b’ in an anticlockwise or clockwise direction.

  • Check the length of the string ‘a’ and string ‘b’.

  • Find the substring of string ‘b’ by rotating two places anticlockwise.

  • Now check if the resultant substring is equal to string ‘a’ or not and return true if it equals.

  • Find the substring of string ‘b’ by rotating it in two places in a clockwise direction.

  • Now check if the resultant substring is equal to string ‘a’ or not and return true if it equals.

  • If the strings are not equal, then return false.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
bool checkRotated(string str1, string str2){
   if (str1.length() != str2.length())
      return false;
   if(str1.length() <= 2 || str2.length() <= 2)
      return (str1 == str2);
   string s1= str2.substr(str2.size()-2, str2.size());
   string s2= str2.substr(0,str2.size()-2);
   string s3= s1+s2;
   if(s3==str1)
      return true;
   string s4= str2.substr(2,str2.size());
   string s5= str2.substr(0,2);
   string s6= s4+s5;
   if(s6==str1)
      return true;
   return false;
}
int main(){
   string a= "google";
   string b="legoog";
   cout<<checkRotated(a,b)<<endl;
   return 0;
}

Output

If we will run the above code, it will print the output as,

1

Since the output of the above code is “True”, it will print ‘1’.

Updated on: 05-Feb-2021

381 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements