From aa47900a52e58eeef20b581f2476e726b50a9c9d Mon Sep 17 00:00:00 2001 From: Rajender Thota Date: Mon, 12 May 2025 23:20:34 -0400 Subject: [PATCH 1/2] Update README_EN.md 3335. Total Characters in String After Transformations I solution --- .../README_EN.md | 145 +++++++++++++++++- 1 file changed, 141 insertions(+), 4 deletions(-) diff --git a/solution/3300-3399/3335.Total Characters in String After Transformations I/README_EN.md b/solution/3300-3399/3335.Total Characters in String After Transformations I/README_EN.md index dfc8513a27e26..0dd6e98d202cc 100644 --- a/solution/3300-3399/3335.Total Characters in String After Transformations I/README_EN.md +++ b/solution/3300-3399/3335.Total Characters in String After Transformations I/README_EN.md @@ -107,7 +107,27 @@ 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 1 @@ -117,25 +137,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> dp(t + 1, vector(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(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 +} ``` From e94583c01d364b793d1d54c7bc8e9704d502ae5e Mon Sep 17 00:00:00 2001 From: rajenderthota <8196242+rajenderthota@users.noreply.github.com> Date: Tue, 13 May 2025 03:23:00 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- .../README_EN.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/solution/3300-3399/3335.Total Characters in String After Transformations I/README_EN.md b/solution/3300-3399/3335.Total Characters in String After Transformations I/README_EN.md index 0dd6e98d202cc..2384f51cfaa43 100644 --- a/solution/3300-3399/3335.Total Characters in String After Transformations I/README_EN.md +++ b/solution/3300-3399/3335.Total Characters in String After Transformations I/README_EN.md @@ -107,6 +107,7 @@ 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: @@ -128,6 +129,7 @@ To solve this problem efficiently, we cannot simulate every transformation by bu 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 1 @@ -170,7 +172,7 @@ class Solution: 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