Shift strings Circular left and right in JavaScript


The main objective for the problem statement is to perform a circular shift on the strings in Javascript. The circular shifts can be left shirt or right shift. And implement this solution in Javascript.

Understanding the Problem

The problem at hand is to shift the strings circularly left and right with the help of Javascript functionalities. Circular shifting means we have to move the characters of the given string in a circular manner in which the characters that are to be shifted beyond the boundaries of the string and it should reappear at the opposite end.

Logic for the given Problem

To solve this problem we will implement two functions. The first function will shift the characters to the left side and the second function will shift the characters to the right side. Both the functions will take a string and the number of positions to shift as input.

The first function will calculate the effective number of positions by taking the modulus of the given positions with the length of the string. Then we will use the substring method to extract the portion of the string and concatenate the portion of the string from the beginning till the computed positions.

The second function will also do the same steps as the first function but in this function we will extract substrings in reverse order. So we will calculate the effective number of positions with the help of modulus. And lastly concatenate the portion with the string from the beginning till the difference between the string length and computed positions.

Algorithm

Step 1: As we have discussed above, we will create two functions separately. So in this step we will define the first function and give it a name as shiftLeft and this function takes two parameters first is string and second is position. The string is the input string for which we are performing this task and positions are the effective number of positions.

Step 2: After defining the function, we will calculate the number of positions by taking the modulus of the given positions with the length of the given string.

Step 3: So in this phase we will use the substring method to extract the portion of the string which will be starting from the calculated positions until the end of the string. And concatenate it with the portion of the string from the starting till the calculated positions.

Step 4: Define the second function to shift the characters of the string to right. And similar to the above function this function also takes two parameters string and the positions.

Step 5: As this function also follows a similar process but in this function we will extract the string in reverse order. So we will calculate the effective number of positions with the help of modulus.

Step 6: Then we will extract the portion of the string beginning from the difference between the string length and the calculated positions until the end of the string.

Step 7: After that we will concatenate the portion of the string from the beginning till the difference between the string length and the computed positions.

Example

//Function to do circular shift left
function shiftLeft(str, positions) {
   // handle positions larger than string length
   positions = positions % str.length;
   return str.substring(positions) + str.substring(0, positions);
}

//Function to do circular shift right
function shiftRight(str, positions) {
   // handle positions larger than string length
   positions = positions % str.length;
   return str.substring(str.length - positions) + 
str.substring(0, str.length - positions);
}

const originalStr = 'Hello Tutorials Point';
const leftShifted = shiftLeft(originalStr, 3);
const rightShifted = shiftRight(originalStr, 2);

console.log(leftShifted);
console.log(rightShifted);

Output

lo Tutorials PointHel
ntHello Tutorials Poi

Complexity

As we have created two functions to do the tasks of shifting the characters left and right side in circular motion. Both the functions have O(n) time complexity, in which n is the size of the given input string. And the space consumed by both the functions is also O(n). Because we have created a new string of length n to concatenate the substrings which requires an extra amount of space.

Conclusion

The code we have implemented is successfully shifting the characters of the string in circular left and circular right sides in Javascript. The time and space complexities are linear and proportional to the input size of the string.

Updated on: 16-Aug-2023

822 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements