Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created May 13, 2025 18:44
Show Gist options
  • Save Ifihan/1296a77f3e72b00dcd723854adc43daa to your computer and use it in GitHub Desktop.
Save Ifihan/1296a77f3e72b00dcd723854adc43daa to your computer and use it in GitHub Desktop.
Total Characters in String After Transformations I

Question

Approach

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.

Implementation

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

image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment