Open In App

Javascript Program to Check if a string can be obtained by rotating another string d places

Last Updated : 19 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right).

Examples: 

Input: str1 = “abcdefg”, str2 = “cdefgab”, d = 2 
Output: Yes 
Rotate str1 2 places to the left.

Input: str1 = “abcdefg”, str2 = “cdfdawb”, d = 6 
Output: No 
 

Approach: An approach to solve the same problem has been discussed here. In this article, reversal algorithm is used to rotate the string to the left and to the right in O(n). If any one of the rotations of str1 is equal to str2 then print Yes else print No.

Below is the implementation of the above approach:  

Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to reverse an array from left
// index to right index (both inclusive)
function ReverseArray(arr, left, right)
{
    var temp;
    while (left < right)
    {
        temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        left++;
        right--;
    }
}
 
// Function that returns true if str1 can be
// made equal to str2 by rotating either
// d places to the left or to the right
function RotateAndCheck(str1, str2, d)
{
    if (str1.length !== str2.length)
        return false;
     
    // Left Rotation string will contain
    // the string rotated Anti-Clockwise
    // Right Rotation string will contain
    // the string rotated Clockwise
    var left_rot_str1 = [];
    var right_rot_str1 = [];
    var left_flag = true,
    right_flag = true;
    var str1_size = str1.length;
     
    // Copying the str1 string to left rotation string
    // and right rotation string
    for(var i = 0; i < str1_size; i++)
    {
        left_rot_str1.push(str1[i]);
        right_rot_str1.push(str1[i]);
    }
     
    // Rotating the string d positions to the left
    ReverseArray(left_rot_str1, 0, d - 1);
    ReverseArray(left_rot_str1, d, str1_size - 1);
    ReverseArray(left_rot_str1, 0, str1_size - 1);
     
    // Rotating the string d positions to the right
    ReverseArray(right_rot_str1, 0, str1_size - d - 1);
    ReverseArray(right_rot_str1, str1_size - d,
                 str1_size - 1);
    ReverseArray(right_rot_str1, 0, str1_size - 1);
     
    // Comparing the rotated strings
    for(var i = 0; i < str1_size; i++)
    {
        // If cannot be made equal with left rotation
        if (left_rot_str1[i] !== str2[i])
        {
            left_flag = false;
        }
         
        // If cannot be made equal with right rotation
        if (right_rot_str1[i] !== str2[i])
        {
            right_flag = false;
        }
    }
     
    // If both or any one of the rotations
    // of str1 were equal to str2
    if (left_flag || right_flag)
        return true;
         
    return false;
}
 
// Driver code
var str1 = "abcdefg";
var str2 = "cdefgab";
 
// d is the rotating factor
var d = 2;
 
// In case length of str1 < d
d = d % str1.length;
 
if (RotateAndCheck(str1, str2, d))
    document.write("Yes");
else
    document.write("No");
     
// This code is contributed by rdtank
 
</script>


 
 

Output: 

Yes

 

 Time Complexity: O(n)

Approach: In this approach we use temp string which is long string with multiplicity of str1 by 2. If we want to rotate the string by d place then we slice the string from n places starting from d. Similarly we rotate in right, For right rotation we use d with value (length of str1) – d. If any one of the rotations of str1 is equal to str2 then print Yes else print No.

Below is the implementation of the above approach:  

Javascript




<script>
 
// Function that returns true if str1 can be
// made equal to str2 by rotating either
// d places to the left or to the right
function RotateAndCheck(str1, str2, d)
{
    if (str1.length !== str2.length)
        return false;
     
    // Making temp to slice the rotated string
    var temp = str1 + str1;
    var temp2 = str2 + str2;
 
    // Index to slice the string to give rotation
    var len = str1.length
    var pos1 = len - d  ;
     
    // Slicing the string
    var left_rot_str1 = temp.slice(d, d + len)
    var right_rot_str2 = temp2.slice(pos1, pos1+len );
        
    // Flags check equality of rotation of string to both the string
    var flag = left_rot_str1 == str2 ;
    var flag2 = right_rot_str2 == str1;
      
     // Returning
    if(flag || flag2) return true;
    return false;
}
 
// Driver code
var str1 = "abcdefg";
var str2 = "cdefgab";
 
// d is the rotating factor
var d = 2;
 
// In case length of str1 < d
d = d % str1.length;
 
if (RotateAndCheck(str1, str2, d))
    console.log("Yes");
else
    console.log("No");
</script>


Output:

Yes 

Time Complexity: O(n), where n is the size of the given string.

Please refer complete article on Check if a string can be obtained by rotating another string d places for more details!
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads