You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/3300-3399/3335.Total Characters in String After Transformations I/README_EN.md
+141-4Lines changed: 141 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -107,7 +107,27 @@ tags:
107
107
<!-- description:end -->
108
108
109
109
## Solutions
110
+
To solve this problem efficiently, we cannot simulate every transformation by building the actual string (as the length may grow exponentially). Instead, we track the length of each character's transformation result using dynamic programming.
110
111
112
+
💡 Idea:
113
+
114
+
Let dp[i][c] represent the length of the result string after i transformations of character c.
115
+
116
+
If c == 'z', after one transformation, it becomes "ab" → so length becomes:
117
+
118
+
dp[i][z] = dp[i-1][a] + dp[i-1][b]
119
+
120
+
If c != 'z', then:
121
+
122
+
dp[i][c] = dp[i-1][next(c)]
123
+
124
+
✅ Steps:
125
+
126
+
Initialize dp[0][c] = 1 for all characters, because 0 transformation means the character stays as-is.
127
+
128
+
For each transformation step i from 1 to t, compute the length for each character.
129
+
130
+
For the final result, sum dp[t][s.charAt(i)] for all characters in s.
0 commit comments