Skip to main content
Back to problems
Leetcode
Medium
Strings
Math
Dynamic Programming
Total Characters In String After Transformations I

Count how many characters remain after repeatedly transforming a string under fixed replacement rules.

Acceptance 100%
Problem Statement

You are given a string and a transformation rule that is applied repeatedly for a fixed number of rounds. In each round, every character is replaced according to the rule, which changes the string length over time. Your task is to determine the total number of characters after all rounds of transformation.

Because the string can grow very quickly, focus on counting the length rather than constructing the full transformed string whenever possible.

Input Format

  • A string representing the initial text.
  • An integer kk representing the number of transformation rounds.
  • The replacement rule is implicitly defined by the problem statement on the platform version.

Output Format

Return the total number of characters present after applying the transformation exactly kk times.

Constraints

  • The string may grow exponentially with the number of rounds.
  • The answer may need to be computed modulo a large constant in the original problem setting.
  • Use an approach that avoids building the full intermediate strings when the growth becomes large.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "a", k = 1

Output

2

Explanation

After one transformation, the single character expands according to the rule, so the total length becomes 2.

Example 2

Input

s = "ab", k = 2

Output

4

Explanation

Apply the transformation twice and count the resulting characters instead of writing the entire final string.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.