Total Characters in String After Transformations II - Problem
Transform and Count: The Expanding String Challenge

Imagine you have a magical string where each character can multiply into multiple consecutive letters! Given a string s of lowercase English letters, you need to perform exactly t transformations and count the final length.

🔄 Transformation Rules:
• Each character s[i] is replaced by the next nums[s[i] - 'a'] consecutive characters in the alphabet
• The alphabet wraps around: after 'z' comes 'a'

📝 Examples:
• If s[i] = 'a' and nums[0] = 3, then 'a' becomes "bcd"
• If s[i] = 'y' and nums[24] = 3, then 'y' becomes "zab" (wrapping around)

🎯 Goal: Return the length of the string after exactly t transformations, modulo 109 + 7.

This is a challenging problem that combines string manipulation, mathematical optimization, and dynamic programming concepts!

Input & Output

example_1.py — Basic Transformation
$ Input: s = "ab", t = 1, nums = [1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Output: 3
💡 Note: After 1 transformation: 'a' becomes 'b' (nums[0]=1), 'b' becomes 'cd' (nums[1]=2). Result: "b" + "cd" = "bcd" with length 3.
example_2.py — Multiple Transformations
$ Input: s = "a", t = 2, nums = [2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Output: 2
💡 Note: T1: 'a'→'bc' (nums[0]=2). T2: 'b'→'c' (nums[1]=1), 'c'→'d' (nums[2]=1). Final: "cd", length=2.
example_3.py — Wrapping Around
$ Input: s = "z", t = 1, nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3]
Output: 3
💡 Note: After 1 transformation: 'z' becomes the next 3 characters, which wraps around: 'z'→'abc' (since after z comes a). Final string: "abc" with length 3.

Constraints

  • 1 ≤ s.length ≤ 105
  • s consists of lowercase English letters
  • 1 ≤ t ≤ 109
  • nums.length == 26
  • 1 ≤ nums[i] ≤ 3
  • Result must be returned modulo 109 + 7

Visualization

Tap to expand
Total Characters in String After Transformations II INPUT String s = "ab" a b t = 1 (transformations) nums array (26 values): a 1 b 2 c 1 d 1 ... nums[0]=1: 'a' expands to 1 char nums[1]=2: 'b' expands to 2 chars Transformation Rule: Each char s[i] becomes next nums[s[i]-'a'] consecutive letters (wraps: z-->a) ALGORITHM STEPS 1 Count Initial Chars freq[a]=1, freq[b]=1 2 Build Transform Matrix 26x26 matrix M M[i][j] = 1 if j is in range [i+1, i+nums[i]] mod 26 M[0][1]=1 (a-->b) M[1][2]=M[1][3]=1 (b-->cd) 3 Matrix Exponentiation Compute M^t (t=1 here) 4 Multiply and Sum result = freq * M^t 'a' (1 char) --> 'b' (1 char) 'b' (1 char) --> 'cd' (2 chars) Total after t=1: 1+2+2=5 FINAL RESULT String Transformation: Before (t=0): a b After (t=1): b c d c d "ab" --> "b" + "cd" = "bcdcd" OUTPUT 5 OK Key Insight: Matrix exponentiation reduces O(26*t) to O(26^3 * log(t)). Each character's expansion can be modeled as a linear transformation. The 26x26 transition matrix M captures how each letter maps to others. Computing M^t via fast exponentiation and multiplying by initial frequency vector gives final counts mod 10^9+7. TutorialsPoint - Total Characters in String After Transformations II | Optimal Solution
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
27.5K Views
Medium-High Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen