Apply Circular Character Rolls to String Prefixes

Quick Overview

Given a lowercase string and a list of roll lengths, each roll cyclically increments the first roll[i] letters, with z wrapping around to a, and the task is to return the final string. It tests whether you can count overlapping prefix updates in linear time when the string and the roll list each hold up to 100,000 entries.

Apply Circular Character Rolls to String Prefixes

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a string `s` of lowercase English letters and an integer array `roll`. For each `roll[i]`, in order, increment the first `roll[i]` characters of the string cyclically: `'a'` becomes `'b'`, `'b'` becomes `'c'`, and so on, and `'z'` becomes `'a'`. Return the resulting string after all roll operations have been applied. ### Function Signature ```python def circular_roll(s: str, roll: list[int]) -> str: ``` ### Constraints - `1 <= len(s) <= 10^5` - `1 <= len(roll) <= 10^5` - `1 <= roll[i] <= len(s)` - `s` consists of lowercase English letters only. ### Examples ```text Input: s = "abc", roll = [1,2,3] Output: "ddd" ``` The rolls take `"abc"` to `"bbc"`, then `"ccc"`, then `"ddd"`. Index 0 is rolled three times, index 1 twice and index 2 once, so each letter lands on `'d'`. ```text Input: s = "zzz", roll = [2,3] Output: "bba" ``` The first roll turns `"zzz"` into `"aaz"` because both rolled letters wrap from `'z'` to `'a'`. The second roll covers all three letters and gives `"bba"`.

Overview: Given a lowercase string and a list of roll lengths, each roll cyclically increments the first roll[i] letters, with z wrapping around to a, and the task is to return the final string. It tests whether you can count overlapping prefix updates in linear time when the string and the roll list each hold up to 100,000 entries.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a string s of lowercase English letters and an integer array roll. For each roll[i], in order, increment the first roll[i] characters of the string cyclically: 'a' becomes 'b', 'b' becomes 'c', and so on, and 'z' becomes 'a'.

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

Function Signature

def circular_roll(s: str, roll: list[int]) -> str:

Constraints

  • 1 <= len(s) <= 10^5
  • 1 <= len(roll) <= 10^5
  • 1 <= roll[i] <= len(s)
  • s consists of lowercase English letters only.

Examples

Input: s = "abc", roll = [1,2,3]
Output: "ddd"

The rolls take "abc" to "bbc", then "ccc", then "ddd". Index 0 is rolled three times, index 1 twice and index 2 once, so each letter lands on 'd'.

Input: s = "zzz", roll = [2,3]
Output: "bba"

The first roll turns "zzz" into "aaz" because both rolled letters wrap from 'z' to 'a'. The second roll covers all three letters and gives "bba".