Engineering platform~12 min2s · 256 MBtarget O(n + m) time, O(n) spacestrings
You work on a smart display that shows a short text in a loop. The text scrolls one letter at a time, and the letter that leaves one end comes back at the other end. A press can scroll the text left or right by many letters at once.
Your team lead asks: "After a series of presses, what does the display show?" The text can have 100,000 letters, and the log can hold 100,000 presses.
Write rotate_text(text, moves).
text is the starting text, made of lowercase letters.moves[i] is press i. A positive number scrolls left by that many letters: the first letter moves to the end, once for each step. A negative number scrolls right by its size: the last letter moves to the front, once for each step.Input: text = "abcdef", moves = [2, -1, 3]
Output: "efabcd"Apply the presses one by one.
| Press | Display after it |
|---|---|
| 2 | cdefab |
| -1 | bcdefa |
| 3 | efabcd |
The three presses together scroll left by 2 - 1 + 3 = 4 letters.
Input: text = "abc", moves = [3]
Output: "abc"Scrolling by the full length of the text brings it back to the start.
Input: text = "xy", moves = [1]
Output: "yx"One step left moves x to the end.
1 ≤ len(text) ≤ 100,000, using the letters a to z1 ≤ len(moves) ≤ 100,000, and -1,000,000,000 ≤ moves[i] ≤ 1,000,000,000You cannot scroll one letter at a time: one press can need a billion steps, and the log can need 100,000,000,000,000 steps. Even rotating the whole text once for every press costs about 100,000 × 100,000 = 10,000,000,000 letter moves. Only the total shift can matter.