I approached this problem by tracking how many letters of each type exist instead of the full string. Each transformation updates the counts: every z adds two a, and all others move forward in the alphabet. After repeating this process t times, the sum of all counts gives the final length.
class Solution:
def lengthAfterTransformations(self, s: str, t: int) -> int:
MOD = 10**9 + 7
counts = [0] * 26
for ch in s:
counts[ord(ch) - ord('a')] += 1
for _ in range(t):
next_counts = [0] * 26
for i in range(25): # 'a' to 'y'
next_counts[i + 1] = (next_counts[i + 1] + counts[i]) % MOD
# For 'z'
next_counts[0] = (next_counts[0] + counts[25] * 2) % MOD
counts = next_counts
return sum(counts) % MOD
But it didn't work. So, I used the editorial
