- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Program to Check if strings are rotations of each other or not
Strings are rotations of each other means two strings that can be rotated to either the right or left direction to obtain the other string. In the right rotation characters of the string shift to their following index and for the zeroth index, it takes the character of the last index assuming the string is in a circle. Left rotation is similar to right rotation but in the opposite direction. We will be given two strings and we have to find whether by rotating the characters of the string we can get another or not.
Input
string1: “abcdef” string2: “cdefab”
Output
Yes
Explanation: We can rotate the first string two times toward the left side and get the second string. String1 after the first rotation will be “bcdefa” and in the next rotation, it will be the same as the second string.
Input
String1: “abcdef” String2: “bcadef”
Output
No
Explanation - The maximum number of rotations that we can rotate a string or array without getting the original string is equal to the length of the given string or the array.
Here, after six rotations we cannot get the string2 from the string1, proving it’s impossible to make both strings equal after the maximum number of rotations.
Naive Approach
In this approach, we will just rotate the given string its length number of times and match with the another given string.
Example
// function to rotate the string in the left direction function left_rotate(str){ // splitting the string and then again joining back str = str.substr(1) + str.substr(0,1); return str; } // function to check if one string is equal to another after certain rotations function check(str1, str2){ // checking the size of both strings if(str1.length != str2.length){ return false; } var k = str1.length while(k--){ if(str1 == str2){ return true; } str1 = left_rotate(str1); } return false; } // defining the strings var str1 = "abcdef" var str2 = "cdefab" 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 by rotating it."); } else{ console.log("No, we cannot obtain the second string from the given string by rotating it."); } // defining the strings str1 = "abcdef" str2 = "bacdef" 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 by rotating it."); } else{ console.log("No, we cannot obtain the second string from the given string by rotating it."); }
Output
The given strings are abcdef and cdefab Yes, we can obtain the second string from the given string by rotating it. The given strings are abcdef and bacdef No, we cannot obtain the second string from the given string by rotating it.
Time and Space Complexity
The time complexity of the above code is O(N*N), where N is the size of the given string.
The space complexity of the above code is O(1), as we are not using any space.
KMP Algorithm
In this program we will use the KMP algorithm to find the rotations, let us move to the code.
Example
// function to check if one string is equal to another using KMP function check(str1, str2){ // checking the size of both strings if(str1.length != str2.length){ return false; } var len = str1.length; // create lps that will hold the longest prefix var lps = Array.from({length: len}, (_, i) => 0); // length of the previous longest prefix suffix var len_p = 0; var i = 1; lps[0] = 0; // the loop calculates lps[i] for i = 1 to n-1 while (i < len) { if (str1.charAt(i) == str2.charAt(len_p)) { lps[i] = ++len_p; i++; } else { if (len_p == 0) { lps[i] = 0; i++; } else { len_p = lps[len_p - 1]; } } } i = 0; // match from that rotating point for(var k = lps[len - 1]; k < len; k++) { if (str2.charAt(k) != str1.charAt(i++)){ return false; } } return true; } // defining the strings var str1 = "abcdef" var str2 = "cdefab" 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 by rotating it."); } else{ console.log("No, we cannot obtain the second string from the given string by rotating it."); } // defining the strings str1 = "abcdef" str2 = "bacdef" 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 by rotating it."); } else{ console.log("No, we cannot obtain the second string from the given string by rotating it."); }
Output
The given strings are abcdef and cdefab Yes, we can obtain the second string from the given string by rotating it. The given strings are abcdef and bacdef No, we cannot obtain the second string from the given string by rotating it.
Time and Space Complexity
For the above program, both the time and space complexity are O(N). We have used the extra space to store the values in lps array.
Conclusion
In this tutorial, we have implemented a JavaScript program to check whether we can obtain the one given string from another given string by rotating the characters of the string in either left of right direction. We have used the naive approach which costs us the O(N*N) time and O(1) space complexity. Also, we have implemented KMP Algorithm with O(N) time and space complexity.