- 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 a string can be obtained by rotating another string d places
Rotating a string means moving the characters of the string to their left or right side by one index and one end may contain the value stored at the other end. We will be given two strings and we have to rotate the second string either in the left or the right direction by given number (d) times and check if both strings are equal or not. We will write proper code with comments, explanations, and detailed discussions on the time and space complexity of the program.
Examples
If we have the given string ‘abcdef’ and the other string is ‘defabc’ and the number of rotations is given as 3.
Output: Yes
Explanation: We can rotate the string to its left by 1 we will get: ‘bcdefa’
In the second rotation string is ‘cdefab’ and in the final third rotation string is ‘defabc’.
Note: Here the side or rotations is not given which means it is not given either we have to rotate the string on the left side or the right side. In this article, we will implement code to rotate the string in both directions differently. Also, we will use the reversal algorithm to find the result.
Approach for Left Rotation
We will implement the reversal algorithm to rotate the elements of the first string by a given number of times and then check if the rotated string and the second are the same. If they are the same we will return true otherwise return the false.
Example
// function to reverse the given string function left_rotation(str,k){ var len = str.length k = k % len return str.substring(k) + str.substring(0,k); } // defining the function to check if the given string can // be converted to another or not function check(string1, string2, number){ // getting the length of the string1 var len1 = string1.length var len2 = string2.length // if the length of both the given strings is not equal then it is impossible if(len1 != len2){ return false; } // rotating the first string string1 = left_rotation(string1,number); if(string1 == string2){ return true; } return false; } // defining the string var string1 = "abcdef"; var string2 = "defabc"; var number = 3; if(check(string1,string2,number)){ console.log("Yes, we can convert the string '" + string1 + "' to string '" + string2 + "' in the given " + number + " number of rotations "); } else { console.log("No, we cannot convert the string '" + string1 + "' to string '" + string2 + "' in the given " + number + " number of rotations "); }
Output
Yes, we can convert string 'abcdef' to string 'defabc' in the given 3 number of rotations
Time and Space Complexity
The time complexity of the above code is O(N) which is linear as we are traversing over the string only two times. One time to rotate the string and the second time to match with the given string.
The space complexity of the above code is O(1) as we are not using any extra space here.
Approach for Right Rotation
In the right rotation, we will do exactly the same as the previous method but just in the opposite direction by using the substring method to get the substring and attach in a way that we get the right rotation.
Example
// function to reverse the given string function right_rotation(str,k){ var len = str.length k = k % len return str.substring(len-k) + str.substring(0,len-k); } // defining the function to check if the given string can // be converted to another or not function check(string1, string2, number){ // getting the length of the string1 var len1 = string1.length var len2 = string2.length // if the length of both the given strings is not equal then it’s impossible if(len1 != len2){ return false; } // rotating the first string string1 = right_rotation(string1,number); if(string1 == string2){ return true; } return false; } // defining the string var string1 = "abcdef"; var string2 = "defabc"; var number = 3; if(check(string1,string2,number)){ console.log("Yes, we can convert the string '" + string1 + "' to the string '" + string2 + "' in the given " + number + " number of rotations "); } else{ console.log("No, we cannot convert the string '" + string1 + "' to the string '" + string2 + "' in the given " + number + " number of rotations "); }
Output
Yes, we can convert the string 'abcdef' to the string 'defabc' in the given 3 number of rotations
Time and Space Complexity
The time complexity of the above code is O(N) which is linear as we are traversing over the string only two times. One time to rotate the string and the second time to match with the given string.
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 the JavaScript code to rotate a given string in both the left and right direction by the given number of times to match with the other given string. If after rotation the given string is equal to another string then print yes otherwise no. We have implemented the code in O(N) time and O(1) space.