Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Check if a String can be Obtained by Rotating Another String by 2 Places



To check if a string can be obtained by rotating another string by 2 places, we have used Javascript substr() method. We will be understanding the code with an example and step wise explanation of the code.

In this article we are having two strings: str1 and str2, where str1 is the given string and str2 is the rotated string. Our task is to check if the string (str2) can be obtained by rotating another string (str1) by 2 places.

Example

Input: 
str1 = TutorialsPoint
str2 = torialsPointTu

Output: true (with 2 left rotations)

Input: 
str1 = TutorialsPoint
str2 = ntTutorialsPoi

Output: true (with 2 right rotations)

Steps to Check String Rotation by 2 Places

We will be following below mentioned steps to check if the string (str2) can be obtained by rotating another string (str1) by 2 places.

  • We have declared two strings str1 and str2 which stores the given string and rotated string respectively. Then, we have defined three functions which are rotateLeft(), rotateRight() and compare().
  • The rotateLeft() and rotateRight() functions accepts a string str as argument and performs left and right rotation respectively whereas compare() function accepts two string as its argument and compare both the strings.
  • Then we have used substr() method. The str.substr(2) + str.substr(0, 2) lef rotates the string str by 2. The str.substr(2) gets the string from second index to end of string and str.substr(0, 2) get the first two letter of string.
  • Then they are concatenated in reverse order for left rotation. Similarly, rotateRight performs right rotation on string str.
  • Inside compare() function, first we check the length of both string if they are equal using if/else statement. Then we have used rotateLeft(str1) and rotateRight(str1) to rotate the given string str1.
  • After rotation of given string, we compare it with str2. If both are equal then str2 can be obtained by rotating str1 by 2 places.
  • At the end, we have called the function using if/else statement to compare and print the output.

Example

Here is a complete example code implementing above mentioned steps to Check if a string can be obtained by rotating another string by 2 places.

let str1 = "TutorialsPoint"
let str2 = "torialsPointTu"
console.log("Given Strings: " + str1 + " and " + str2);

function rotateLeft(str) {
    str = str.substr(2) + str.substr(0, 2);
    return str;
}

function rotateRight(str) {
    let len = str.length
    str = str.substr(len - 2) + str.substr(0, len - 2)
    return str;
}

function compare(str1, str2) {
    if (str1.length != str2.length) {
        return false;
    }

    let l_str = rotateLeft(str1);
    let r_str = rotateRight(str1);
    if (r_str == str2 || l_str == str2) {
        return true;
    } else {
        return false;
    }
}

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

Time Complexity and Space Complexity

Time complexity for the above code is O(n), where n is the length of the string and the space complexity is also O(n).

Conclusion

In this article, we have implemented a JavaScript program to Check if a string can be obtained by rotating another string by 2 places. We have used the substr() method to split the string and then joined it back to make the two rotations.

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2025-01-15T13:00:36+05:30

263 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements