Watch 10 video solutions for Perform String Shifts, a easy level problem involving Array, Math, String. This walkthrough by Techdose has 17,109 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s containing lowercase English letters, and a matrix shift, where shift[i] = [directioni, amounti]:
directioni can be 0 (for left shift) or 1 (for right shift).amounti is the amount by which string s is to be shifted.s and append it to the end.s and add it to the beginning.Return the final string after all operations.
Example 1:
Input: s = "abc", shift = [[0,1],[1,2]] Output: "cab" Explanation: [0,1] means shift to left by 1. "abc" -> "bca" [1,2] means shift to right by 2. "bca" -> "cab"
Example 2:
Input: s = "abcdefg", shift = [[1,1],[1,1],[0,2],[1,3]] Output: "efgabcd" Explanation: [1,1] means shift to right by 1. "abcdefg" -> "gabcdef" [1,1] means shift to right by 1. "gabcdef" -> "fgabcde" [0,2] means shift to left by 2. "fgabcde" -> "abcdefg" [1,3] means shift to right by 3. "abcdefg" -> "efgabcd"
Constraints:
1 <= s.length <= 100s only contains lower case English letters.1 <= shift.length <= 100shift[i].length == 2directioni is either 0 or 1.0 <= amounti <= 100Problem Overview: You receive a string and a list of shift operations. Each operation moves characters either left or right by a given amount. The goal is to apply all shifts and return the final string.
The key detail is that multiple shifts accumulate. Instead of physically shifting the string every time, you can combine the shifts mathematically and perform a single rotation.
Approach 1: Direct Simulation (O(n * m) time, O(n) space)
The most straightforward method processes each operation one by one and updates the string immediately. For a left shift, move the first k characters to the end. For a right shift, move the last k characters to the front. This can be implemented using substring slicing or manual concatenation.
Each shift requires rebuilding the string, which costs O(n) time. If there are m operations, the total complexity becomes O(n * m). Space complexity stays O(n) due to intermediate string copies. This method is easy to implement and works fine when the number of operations is small, but it becomes inefficient when many shifts are applied.
Approach 2: Net Shift Calculation (Simulation Optimization) (O(n + m) time, O(n) space)
A better strategy combines all shifts into a single net rotation. Iterate through the operations and track the total shift amount: treat left shifts as negative movement and right shifts as positive. After processing all operations, reduce the result using modulo n where n is the string length.
This converts multiple rotations into one final shift. If the final shift is positive, perform a right rotation; if negative, perform a left rotation. The rotation itself is done using substring slicing or concatenation: split the string and reorder the pieces.
The algorithm scans the operations once (O(m)) and performs one final rotation (O(n)). Total time complexity becomes O(n + m) with O(n) space for the resulting string. This approach relies on simple arithmetic from math and efficient manipulation of string segments.
Recommended for interviews: Interviewers expect the net shift approach. Direct simulation shows you understand the mechanics of rotation, but combining operations demonstrates stronger reasoning about cumulative effects. Recognizing that shifts can cancel each other out is the key insight. The implementation is simple once you compute the final offset and rotate the array-like structure of characters.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Simulation | O(n * m) | O(n) | When the number of operations is small or for quick prototyping |
| Net Shift Calculation (Optimized Simulation) | O(n + m) | O(n) | General case with many shift operations; preferred interview solution |