Skip to the code
cf.
+25 XP

#58 Shift Letters by Ranges

medium

A text-effects tool applies a list of shift operations to a lowercase string s. Each operation [start, end, direction] shifts every letter from index start to end (both included) one place forward in the alphabet if direction is 1, or one place back if it is 0. Shifting wraps: z forward is a, and a back is z.

Write shiftLetters(s, shifts) that returns the string after all operations.

Example 1

Input: s = "code", shifts = [[0,1,1],[1,3,0],[0,3,1]]
Output: "epde"

Index 0 moves forward twice (c to e), index 1 moves forward once overall (o to p), and indexes 2 and 3 end where they started.

Example 2

Input: s = "zz", shifts = [[0,1,1]]
Output: "aa"

z shifted forward wraps to a.

Constraints

  • 1 <= s.length <= 5 * 10^4
  • 0 <= shifts.length <= 5 * 10^4
  • 0 <= start <= end < s.length
  • direction is 0 or 1.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).