Skip to content

Commit aa47900

Browse files
Update README_EN.md
3335. Total Characters in String After Transformations I solution
1 parent 356cd7b commit aa47900

File tree

1 file changed

+141
-4
lines changed
  • solution/3300-3399/3335.Total Characters in String After Transformations I

1 file changed

+141
-4
lines changed

solution/3300-3399/3335.Total Characters in String After Transformations I/README_EN.md

Lines changed: 141 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,27 @@ tags:
107107
<!-- description:end -->
108108

109109
## 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.
110111

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.
111131
<!-- solution:start -->
112132

113133
### Solution 1
@@ -117,25 +137,142 @@ tags:
117137
#### Python3
118138

119139
```python
120-
140+
class Solution:
141+
def lengthAfterTransformations(self, s: str, t: int) -> int:
142+
MOD = 10**9 + 7
143+
144+
# dp[i][c] will be the length of character c ('a' to 'z') after i transformations
145+
dp = [[0] * 26 for _ in range(t + 1)]
146+
147+
# Base case: 0 transformations, each character remains as itself
148+
for c in range(26):
149+
dp[0][c] = 1
150+
151+
# Build up dp table for 1 to t transformations
152+
for i in range(1, t + 1):
153+
for c in range(26):
154+
if c == 25: # 'z'
155+
dp[i][c] = (dp[i-1][0] + dp[i-1][1]) % MOD # 'z' → 'a' + 'b'
156+
else:
157+
dp[i][c] = dp[i-1][c + 1] # shift to next character
158+
159+
# Calculate the total length after t transformations for input string s
160+
result = 0
161+
for ch in s:
162+
result = (result + dp[t][ord(ch) - ord('a')]) % MOD
163+
164+
return result
121165
```
122166

123167
#### Java
124168

125169
```java
126-
170+
class Solution {
171+
172+
private static final int MOD = 1_000_000_007;
173+
174+
public int lengthAfterTransformations(String s, int t) {
175+
int[][] dp = new int[t + 1][26]; // dp[i][c] = length of result string after i transformations of char c
176+
177+
// Base case: 0 transformations → length is 1 for each character
178+
for (int c = 0; c < 26; c++) {
179+
dp[0][c] = 1;
180+
}
181+
182+
// Fill DP table
183+
for (int i = 1; i <= t; i++) {
184+
for (int c = 0; c < 26; c++) {
185+
if (c == 25) { // 'z'
186+
dp[i][c] = (dp[i - 1][0] + dp[i - 1][1]) % MOD; // 'z' → "ab"
187+
} else {
188+
dp[i][c] = dp[i - 1][c + 1]; // next character
189+
}
190+
}
191+
}
192+
193+
// Compute total length after t transformations
194+
long result = 0;
195+
for (char ch : s.toCharArray()) {
196+
result = (result + dp[t][ch - 'a']) % MOD;
197+
}
198+
199+
return (int) result;
200+
}
201+
}
127202
```
128203

129204
#### C++
130205

131206
```cpp
132-
207+
class Solution {
208+
public:
209+
int lengthAfterTransformations(string s, int t) {
210+
const int MOD = 1e9 + 7;
211+
vector<vector<int>> dp(t + 1, vector<int>(26, 0));
212+
213+
// Base case: 0 transformations, each character has length 1
214+
for (int c = 0; c < 26; ++c) {
215+
dp[0][c] = 1;
216+
}
217+
218+
// Fill dp table for 1 to t transformations
219+
for (int i = 1; i <= t; ++i) {
220+
for (int c = 0; c < 26; ++c) {
221+
if (c == 25) { // 'z'
222+
dp[i][c] = (dp[i - 1][0] + dp[i - 1][1]) % MOD;
223+
} else {
224+
dp[i][c] = dp[i - 1][c + 1];
225+
}
226+
}
227+
}
228+
229+
// Calculate total length after t transformations
230+
long long result = 0;
231+
for (char ch : s) {
232+
result = (result + dp[t][ch - 'a']) % MOD;
233+
}
234+
235+
return static_cast<int>(result);
236+
}
237+
};
133238
```
134239

135240
#### Go
136241

137242
```go
138-
243+
func lengthAfterTransformations(s string, t int) int {
244+
const MOD = 1_000_000_007
245+
246+
// dp[i][c] = length of character 'a'+c after i transformations
247+
dp := make([][]int, t+1)
248+
for i := range dp {
249+
dp[i] = make([]int, 26)
250+
}
251+
252+
// Base case: 0 transformations → each character has length 1
253+
for c := 0; c < 26; c++ {
254+
dp[0][c] = 1
255+
}
256+
257+
// Build DP table for 1 to t transformations
258+
for i := 1; i <= t; i++ {
259+
for c := 0; c < 26; c++ {
260+
if c == 25 { // 'z'
261+
dp[i][c] = (dp[i-1][0] + dp[i-1][1]) % MOD
262+
} else {
263+
dp[i][c] = dp[i-1][c+1]
264+
}
265+
}
266+
}
267+
268+
// Compute the total length after t transformations
269+
result := 0
270+
for _, ch := range s {
271+
result = (result + dp[t][ch-'a']) % MOD
272+
}
273+
274+
return result
275+
}
139276
```
140277

141278
<!-- tabs:end -->

0 commit comments

Comments
 (0)