Problem · String

Prefix-Length Letter Shifts

Learn this problem
MediumChewy logoChewyFULLTIMEPHONE SCREEN

Problem statement

You are given a lowercase string s and an array shifts. Process shifts from left to right. For each value k, shift each of the first k letters of the current string forward by one alphabet position.

Letters wrap from z to a. Every prefix length is inclusive in the range 1 through s.length(). Return the final string after every shift is applied.

Function

shiftPrefixes(s: String, shifts: int[]) → String

Examples

Example 1

s = "abcd"shifts = [1,2,3,4]return = "eeee"

The successive strings are bbcd, cccd, dddd, and eeee.

Example 2

s = "aez"shifts = [1,1,3]return = "dfa"

The successive strings are bez, cez, and dfa; the final z wraps to a.

Example 3

s = "abc"shifts = [2,2,2]return = "dec"

The successive strings are bcc, cdc, and dec.

Constraints

  • 1 <= s.length() <= 200000.
  • 1 <= shifts.length <= 200000.
  • s contains only lowercase English letters.
  • 1 <= shifts[i] <= s.length().
drafts saved locally
public String shiftPrefixes(String s, int[] shifts) {
    // Write your code here.
}
s"abcd"
shifts[1,2,3,4]
expected"eeee"
checking account