
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript Program for Left Rotation and Right Rotation of a String
Left rotation of a string means anti-clockwise movement of the given number of characters from the prefix side and add them to the suffix side. Similarly, right rotation of a string means clockwise movement of the characters of the given string but just opposite of the left rotation and given number of characters are picked from the suffix and added to the prefix of the string. In this article, we will implement the JavaScript program for the left rotation and right rotation of a given string.
Introduction to Problem
In this problem, we are given a string and a number. The number is indicating how many times we have to rotate the string in either direction. As we have got some idea about the clockwise and anti-clockwise rotation means getting some character from the given string either from the starting or prefix side or from the ending or suffix side and removing from them and adding at the end of the string.
The length of the string and the frequency of the characters in the given string not changes, only the permutation of the current string change.
For example, we are given a string: apple
Then the left rotations of the given string will be as −
pplea
pleap
leapp
eappl
apple
We can see the 5th rotation of the given string is same as the current string, so no more further rotation is possible.
The right rotations of the given string will be as −
eappl
leapp
pleap
pplea
apple
We can see that the 5th right rotation of the given string is same as the initial string, so no more different results will be occur if we rotate the string more.
From the above example, we can conclude that by rotating the string either in left or right side its length size number of times will eventually bring the same string as the initial one. So, if the number of rotations are given more than length of the string then we can take mode with it and it will give the exact same answer.
Approach
We have seen the basic example of the left and the right rotation of a string, now let’s move to the step by step approach to get a better understanding of the code that we will implement after that.
First we will, get the given string in a variable and store the number of rotation required in another variable. We can print them also to get a better comparison.
We will create two functions, first for the left rotation and second for the right rotation of the string.
For each function, we will pass the given string and number of rotation variable as the parameters.
In the left rotation function, we will get the two substrings that contains the last k element and second the remaining elements and switch their places.
In the right rotation function, we will get the two substring that contains the first k element and the second with remaining elements and switch their places.
Example
// function for left rotation function left_rotation(str,k){ // getting prefix elements and remaining elements // switiching there place var new_str = str.substr(k) + str.substring(0,k); // printing the rotated string console.log("String after kth left rotation is: " + new_str); } // function for right rotation function right_rotation(str,k){ // getting suffix elements and remaining elements // switiching there place var new_str = str.substr(str.length - k) + str.substring(0,str.length-k); // printing the rotated string console.log("String after kth rigth rotation is: " + new_str); } // given string var str = "apple" var k = 2 // printing the given string console.log("The given string is: " + str); // getting left rotation left_rotation(str,k); // getting right rotation right_rotation(str,k)
Note
In the above program, we are given the ‘k’ or the number of rotation less than the size of the string and if the k is greater than the size of the string then the above code will give the error, but as we have seen in the introduction section the rotation after the length of string number of times is repeated and can be calculated by getting the mode of the current number with the given length of string, to get a safe side we can always do −
k = k % (str.length)
Here, str is the given string.
Time and Space Complexity
The time complexity of the above code is O(N), where N is the size of the given string. In the above code, we are just getting the string character and breaking as well as adding them in different manners makes the time complexity linear.
The space complexity of the above code is O(1), as we are not using any extra space, just storing the one string into the another, that space is used for answer, so no extra space is used.
Conclusion
In this tutorial, we have implemented the JavaScript program for the left rotation and right rotation of a given string. Left rotation of a string means anti-clockwise movement of the given number of characters and right rotation of the string meand clockwise movement of the given number of characters. We have used the concept of the substring by which we are breaking the string and adding the suffix or prefix in another side. The time complexity of the given program is O(N) and space complexity is O(1).
- Related Articles
- JavaScript Program for Longest Subsequence of a Number having Same Left and Right Rotation
- JavaScript Program for Reversal algorithm for right rotation of an array
- Minimize characters to be changed to make the left and right rotation of a string same
- JavaScript Program for Clockwise rotation of Linked List
- JavaScript Program to Find Lexicographically minimum string rotation
- Java Program for Reversal algorithm for right rotation of an array
- JavaScript Program for Reversal algorithm for array rotation
- JavaScript Program for Queries for rotation and Kth character of the given string in constant time
- JavaScript Program for Block swap algorithm for array rotation
- Python Program for array rotation
- Java Program for array rotation
- Golang Program For Array Rotation
- C Program for Program for array rotation?
- Lexicographically minimum string rotation
- Reversal Algorithm for Right Rotation of an Array using C++
