Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Performing shifts within a string in JavaScript
In JavaScript, string shifting involves moving characters from one end of a string to the other. This is commonly used in algorithms and data manipulation tasks where you need to rotate string characters based on specific directions and amounts.
String shifts can be performed in two directions:
Left shift (0): Remove characters from the beginning and append them to the end
Right shift (1): Remove characters from the end and prepend them to the beginning
Understanding Shift Operations
Let's see how individual shifts work:
// Left shift by 1: "abc" -> "bca"
let str = "abc";
let leftShifted = str.substring(1) + str.substring(0, 1);
console.log("Left shift:", leftShifted);
// Right shift by 1: "abc" -> "cab"
let rightShifted = str.substring(str.length - 1) + str.substring(0, str.length - 1);
console.log("Right shift:", rightShifted);
Left shift: bca Right shift: cab
Example Problem
Given a string and an array of shift operations, where each operation is [direction, amount]:
const str = 'abc';
const arr = [[0, 1], [1, 2]];
const performShifts = (str = '', arr = []) => {
if(str.length < 2){
return str;
}
let right = 0;
let left = 0;
// Calculate net shifts
for(let sub of arr){
if(sub[0] == 0){
left += sub[1];
} else {
right += sub[1];
}
}
// If shifts cancel out, return original
if(right === left){
return str;
}
// Apply net right shifts
if(right > left){
right = right - left;
right = right % str.length;
return str.substring(str.length - right) + str.substring(0, str.length - right);
}
// Apply net left shifts
else {
left = left - right;
left = left % str.length;
return str.substring(left) + str.substring(0, left);
}
};
console.log("Original string:", str);
console.log("Shift operations:", arr);
console.log("Result:", performShifts(str, arr));
Original string: abc Shift operations: [ [ 0, 1 ], [ 1, 2 ] ] Result: cab
How It Works
The algorithm optimizes by calculating the net effect of all shifts:
- Sum all left shifts and right shifts separately
- Calculate the net direction (right - left)
- Use modulo to handle shifts larger than string length
- Apply the final shift using
substring()
Step-by-Step Trace
// Tracing the example: str = "abc", arr = [[0,1], [1,2]]
console.log("Step 1 - Original:", "abc");
console.log("Step 2 - Left shift by 1:", "bca");
console.log("Step 3 - Right shift by 2:", "cab");
// But the algorithm optimizes:
console.log("\nOptimized calculation:");
console.log("Left shifts total:", 1);
console.log("Right shifts total:", 2);
console.log("Net right shifts:", 2 - 1, "= 1");
console.log("Final result:", "cab");
Step 1 - Original: abc Step 2 - Left shift by 1: bca Step 3 - Right shift by 2: cab Optimized calculation: Left shifts total: 1 Right shifts total: 2 Net right shifts: 1 = 1 Final result: cab
Conclusion
String shifting in JavaScript can be efficiently implemented by calculating net shifts rather than performing each operation individually. This approach uses modulo arithmetic to handle large shift amounts and substring() for the final rotation.
