The Perform String Shifts problem gives you a string and a list of shift operations (direction: 0=left, 1=right, amount). Apply all shifts to the string and return the result. This easy coding problem has an O(n) insight: accumulate the total net shift and perform a single rotation. The array, math, and string interview pattern is demonstrated.
Goldman Sachs asks this to test the key optimization insight: instead of performing each rotation individually (O(nk)), sum all left and right shifts to get the net shift, then do a single string rotation. This reduces O(nk) to O(n).
Net shift accumulation + single rotation. For each shift [dir, amt]: if dir=0 (left), subtract amt; if dir=1 (right), add amt. Compute net_shift % n (handle negative modulo). Rotate: right shift by net_shift ≡ s[-net_shift:] + s[:-net_shift].
s="abcdef", shifts=[[1,1],[1,1],[0,2],[1,3]]. Net = +1+1-2+3 = 3. net_shift = 3. Right rotate by 3: "def" + "abc" = "defabc".
net % n handles this in Python, but not in C++/Java for negative).s[n-k:] + s[:n-k].String rotation problems always reduce to a single rotation by accumulating the net shift. The net shift approach is the critical optimization. Practice string rotation: s[k:] + s[:k] for left rotation, s[-k:] + s[:-k] for right rotation. Be careful with negative modulo in different languages — always ensure the final shift is in [0, n-1].
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find the Divisibility Array of a String | Medium | Solve | |
| Minimum Time Difference | Medium | Solve | |
| Number of Laser Beams in a Bank | Medium | Solve | |
| Determine if Two Events Have Conflict | Easy | Solve | |
| Check If Two String Arrays are Equivalent | Easy | Solve |