Skip to content

3335. Total Characters in String After Transformations I solution in Java python C++ and Go #4403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ tags:

## Solutions

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.

💡 Idea:

Let dp[i][c] represent the length of the result string after i transformations of character c.

If c == 'z', after one transformation, it becomes "ab" → so length becomes:

dp[i][z] = dp[i-1][a] + dp[i-1][b]

If c != 'z', then:

dp[i][c] = dp[i-1][next(c)]

✅ Steps:

Initialize dp[0][c] = 1 for all characters, because 0 transformation means the character stays as-is.

For each transformation step i from 1 to t, compute the length for each character.

For the final result, sum dp[t][s.charAt(i)] for all characters in s.

<!-- solution:start -->

### Solution 1
Expand All @@ -117,25 +139,142 @@ tags:
#### Python3

```python

class Solution:
def lengthAfterTransformations(self, s: str, t: int) -> int:
MOD = 10**9 + 7

# dp[i][c] will be the length of character c ('a' to 'z') after i transformations
dp = [[0] * 26 for _ in range(t + 1)]

# Base case: 0 transformations, each character remains as itself
for c in range(26):
dp[0][c] = 1

# Build up dp table for 1 to t transformations
for i in range(1, t + 1):
for c in range(26):
if c == 25: # 'z'
dp[i][c] = (dp[i-1][0] + dp[i-1][1]) % MOD # 'z' → 'a' + 'b'
else:
dp[i][c] = dp[i-1][c + 1] # shift to next character

# Calculate the total length after t transformations for input string s
result = 0
for ch in s:
result = (result + dp[t][ord(ch) - ord('a')]) % MOD

return result
```

#### Java

```java

class Solution {

private static final int MOD = 1_000_000_007;

public int lengthAfterTransformations(String s, int t) {
int[][] dp = new int[t + 1][26]; // dp[i][c] = length of result string after i transformations of char c

// Base case: 0 transformations → length is 1 for each character
for (int c = 0; c < 26; c++) {
dp[0][c] = 1;
}

// Fill DP table
for (int i = 1; i <= t; i++) {
for (int c = 0; c < 26; c++) {
if (c == 25) { // 'z'
dp[i][c] = (dp[i - 1][0] + dp[i - 1][1]) % MOD; // 'z' → "ab"
} else {
dp[i][c] = dp[i - 1][c + 1]; // next character
}
}
}

// Compute total length after t transformations
long result = 0;
for (char ch : s.toCharArray()) {
result = (result + dp[t][ch - 'a']) % MOD;
}

return (int) result;
}
}
```

#### C++

```cpp

class Solution {
public:
int lengthAfterTransformations(string s, int t) {
const int MOD = 1e9 + 7;
vector<vector<int>> dp(t + 1, vector<int>(26, 0));

// Base case: 0 transformations, each character has length 1
for (int c = 0; c < 26; ++c) {
dp[0][c] = 1;
}

// Fill dp table for 1 to t transformations
for (int i = 1; i <= t; ++i) {
for (int c = 0; c < 26; ++c) {
if (c == 25) { // 'z'
dp[i][c] = (dp[i - 1][0] + dp[i - 1][1]) % MOD;
} else {
dp[i][c] = dp[i - 1][c + 1];
}
}
}

// Calculate total length after t transformations
long long result = 0;
for (char ch : s) {
result = (result + dp[t][ch - 'a']) % MOD;
}

return static_cast<int>(result);
}
};
```

#### Go

```go

func lengthAfterTransformations(s string, t int) int {
const MOD = 1_000_000_007

// dp[i][c] = length of character 'a'+c after i transformations
dp := make([][]int, t+1)
for i := range dp {
dp[i] = make([]int, 26)
}

// Base case: 0 transformations → each character has length 1
for c := 0; c < 26; c++ {
dp[0][c] = 1
}

// Build DP table for 1 to t transformations
for i := 1; i <= t; i++ {
for c := 0; c < 26; c++ {
if c == 25 { // 'z'
dp[i][c] = (dp[i-1][0] + dp[i-1][1]) % MOD
} else {
dp[i][c] = dp[i-1][c+1]
}
}
}

// Compute the total length after t transformations
result := 0
for _, ch := range s {
result = (result + dp[t][ch-'a']) % MOD
}

return result
}
```

<!-- tabs:end -->
Expand Down