Ctrl K

The Growing String

Medium
You are given a string word of lowercase English letters and an integer rounds. In one round every letter of the string is replaced at the same time: - z turns into the two letters ab; - any other letter turns into the next letter of the alphabet (a → b, m → n, and so on). Return the length of the string after exactly rounds rounds. The answer can be huge, so return it modulo 10⁹ + 7.
Example 1
word="xyz", rounds=2 → 5

After round one: y, z, ab gives "yzab". After round two: z, ab, b, c gives "zabbc", which is 5 characters.

Example 2
word="hello", rounds=3 → 5

No letter reaches z within three steps, so the string just shifts and keeps its length of 5.

Example 3
word="a", rounds=27 → 2
Constraints
  • 1 ≤ len(word) ≤ 10⁵
  • word consists of lowercase English letters only
  • 1 ≤ rounds ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLength of a Growing Word HardPieces With Matching Ends MediumMessages Behind Key Presses MediumEven Letter Pieces MediumHow Far a Tweaked Route Can Take You Medium
loading Python…
Input
word="xyz", rounds=2
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 89 tests on the server, hidden ones included.