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 shift 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 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: We will define the first function named shiftLeft that takes two parameters: string and 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: We will use the substring method to extract the portion of the string 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. Similar to the above function, this function also takes two parameters: string and the positions.

Step 5: This function follows a similar process but extracts the string in reverse order. 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.

Implementation

// 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';
console.log('Original String:', originalStr);

const leftShifted = shiftLeft(originalStr, 3);
const rightShifted = shiftRight(originalStr, 2);

console.log('Left shifted by 3:', leftShifted);
console.log('Right shifted by 2:', rightShifted);
Original String: Hello Tutorials Point
Left shifted by 3: lo Tutorials PointHel
Right shifted by 2: ntHello Tutorials Poi

How It Works

For left shift, we move characters from the left side to the right. The characters at positions 0 to (positions-1) move to the end, and the remaining characters move to the beginning.

For right shift, we move characters from the right side to the left. The last 'positions' characters move to the beginning, and the remaining characters follow them.

Complexity

Both functions have O(n) time complexity, where n is the size of the input string. The space consumed by both functions is also O(n) because we create a new string of length n to concatenate the substrings which requires an extra amount of space.

Conclusion

The code successfully shifts the characters of the string in circular left and circular right directions in Javascript. The time and space complexities are linear and proportional to the input size of the string.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements