JavaScript Program to Check if a string can be obtained by rotating another string by 2 places


We have given two strings s1 and s2 and we have to check if is it possible to obtain a string by rotating another string by 2 places. We can rotate the string either in an anti-clockwise or clockwise direction. Here we have to print ‘Yes’ if both the string got match after the rotation of the string by 2 places otherwise, we have to print ‘No’.

Examples

Let us assume we have given two strings s1 and s2 as

Example 1

Input: s1 = “TutorialsPoint”, s2 = “PointTutorials”
Output: No

Explanation: here s2 is the anti-clockwise rotation of string s1 by 9 places and the clockwise rotation of s1 by 5 places. None of them is 2 place rotation so the output is ‘No’.

Example 2:

Input: s1 = “TutorialsPoint”, s2 = “torialsPointTu”
Output: Yes

Explanation: here s2 is the anti-clockwise rotation of string s1 by 2 places and the clockwise rotation of s1 by 12 places. one of them is 2 place rotation so the output is ‘Yes’.

Substring Approach

We have seen example above, now let us see the steps to implement the code −

  • First we will implement a function that will take the string as the parameter and return the left rotation by 2 indexes of the string.

  • In this function, first we will split the string by using the substring method and then join it back.

  • Similar to first method we will define an another function to rotate the string to 2 index right by splitting and return it.

  • Our main work will be defined under a new function, where we will pass the both strings as the parameter and then compare if their length is not same then will return false.

  • We will call to the left rotation and right rotation function and will get them both. After that we will match them, if any of them match with the second string we will print yes otherwise no.

Example

In the below example we check if a string can be obtained by rotating another string by 2 places. Below is the input and expected outputs.

Input: str1 = TutorialsPoint str2 = torialsPointTu

Expected Output: Yes

// function to rotate the string in the left direction
function left_rotate(str){

   // splitting the string and then again joining back
   str = str.substr(2) + str.substr(0,2);
   return str;
}

// function to rotate the string in the right direction
function right_rotate(str){
   // getting the length of the string
   var len = str.length
   // splitting the string and then again joining back
   str = str.substr(len-2) + str.substr(0,len-2)
   return str;
}

// function to check if one string is equal to another after two rotations
function check(str1, str2){

   // checking the size of both strings
   if(str1.length != str2.length){
      return false;
   }
   
   // getting the left rotation of given string
   var l_str = left_rotate(str1);
   
   // getting the right rotation of given string
   var r_str = right_rotate(str1);
   if(r_str == str2 || l_str == str2){
      return true;
   } else {
      return false;
   }
}

// defining the strings
var str1 = "TutorialsPoint"
var str2 = "torialsPointTu"
console.log("The given strings are " + str1 + " and " + str2);

// calling the function
if(check(str1,str2)){
   console.log("Yes, we can obtain the second string from the given string after two rotations");
}
else{
   console.log("No, we cannot obtain the second string from the given string after two rotations");
}

Output

The given strings are TutorialsPoint and torialsPointTu
Yes, we can obtain the second string from the given string after two rotations

Time Complexity and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given strings. We have split the given string two times and we have also compared the given strings to make the time complexity of O(N).

The space complexity of the above code is O(1), as we are not using any extra space here.

Conclusion

In this tutorial, we have implemented a JavaScript program to check if two given strings can become equal by rotating one of them in either an anti-clockwise or clockwise direction. We used the substring method to split the string and then joined it back to make the two rotations. The time complexity of the code is O(N), where we have not used any extra space.

Updated on: 13-Apr-2023

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements