Problem · String

Roll the String

Learn this problem
MediumMicrosoft logoMicrosoftINTERNOA
See Microsoft hiring insights

Problem statement

A single roll operation increments each character by one cyclically within the lowercase English alphabet. For example, a becomes b, b becomes c, and z becomes a.

Given a string s and an integer array roll, process every value roll[i] in array order. For each value, roll the first roll[i] characters of s once.

Return the resulting string after all roll operations have been applied.

Function

rollTheString(s: String, roll: int[]) → String

Examples

Example 1

s = "abz"roll = [3,2,1]return = "dda"

Apply the rolls in order:

  1. roll[0] = 3: roll all three characters, so abz becomes bca.
  2. roll[1] = 2: roll the first two characters, so bca becomes cda.
  3. roll[2] = 1: roll the first character, so cda becomes dda.

The final value of s is dda.

Constraints

  • 1 <= s.length <= 10^5.
  • 1 <= roll.length <= 10^5.
  • s contains only lowercase English letters.
  • 1 <= roll[i] <= s.length for every valid index i.

More Microsoft problems

drafts saved locally
public String rollTheString(String s, int[] roll) {
    // write your code here
}
s"abz"
roll[3,2,1]
expected"dda"
checking account