oneby7

Arrays and Hashing Difference arrays

595. Shifting Letters II

Medium

You are given a string s of lowercase English letters and a list shifts. Each shift is a triple [start, end, direction]. It moves every letter of s from index start to index end, both ends included, one step through the alphabet: forward when direction is 1, and backward when direction is 0.

The alphabet wraps around. A forward step turns z into a, and a backward step turns a into z.

Apply every shift and return the final string. Answers are shown in double quotes.

Example 1

Input: s = "away" shifts = [[1, 3, 1], [0, 1, 0], [2, 3, 1]]

Output: "zwca"

Explanation: Index 0 gets one backward step, so a wraps to z. Index 1 gets one step each way, so w stays. Indexes 2 and 3 get two forward steps each: a becomes c, and y passes z and wraps to a.

Example 2

Input: s = "b", shifts = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

Output: "y"

Explanation: Three backward steps take b to a, then to z, then to y.

Example 3

Input: s = "dog" shifts = [[0, 2, 1], [0, 1, 0], [2, 2, 0]]

Output: "dog"

Explanation: Every letter gets one forward step and one backward step, so the string comes back unchanged.

Check your understanding

Work out the answer for this input before you write any code.

s = "cat", shifts = [[1, 2, 0]]

Constraints

  • 1 ≤ s.length ≤ 105
  • s holds only lowercase English letters.
  • 1 ≤ shifts.length ≤ 105
  • shifts[i] = [start, end, direction]
  • 0 ≤ start ≤ end < s.length
  • direction is 0 or 1.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.