3335. Total Characters in String After Transformations I LeetCode Solution
In this guide, you will get 3335. Total Characters in String After Transformations I LeetCode Solution with the best time and space complexity. The solution to Total Characters in String After Transformations I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Total Characters in String After Transformations I solution in C++
- Total Characters in String After Transformations I solution in Java
- Total Characters in String After Transformations I solution in Python
- Additional Resources
Problem Statement of Total Characters in String After Transformations I
You are given a string s and an integer t, representing the number of transformations to perform. In one transformation, every character in s is replaced according to the following rules:
If the character is ‘z’, replace it with the string “ab”.
Otherwise, replace it with the next character in the alphabet. For example, ‘a’ is replaced with ‘b’, ‘b’ is replaced with ‘c’, and so on.
Return the length of the resulting string after exactly t transformations.
Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: s = “abcyy”, t = 2
Output: 7
Explanation:
First Transformation (t = 1):
‘a’ becomes ‘b’
‘b’ becomes ‘c’
‘c’ becomes ‘d’
‘y’ becomes ‘z’
‘y’ becomes ‘z’
String after the first transformation: “bcdzz”
Second Transformation (t = 2):
‘b’ becomes ‘c’
‘c’ becomes ‘d’
‘d’ becomes ‘e’
‘z’ becomes “ab”
‘z’ becomes “ab”
String after the second transformation: “cdeabab”
Final Length of the string: The string is “cdeabab”, which has 7 characters.
Example 2:
Input: s = “azbk”, t = 1
Output: 5
Explanation:
First Transformation (t = 1):
‘a’ becomes ‘b’
‘z’ becomes “ab”
‘b’ becomes ‘c’
‘k’ becomes ‘l’
String after the first transformation: “babcl”
Final Length of the string: The string is “babcl”, which has 5 characters.
Constraints:
1 <= s.length <= 105
s consists only of lowercase English letters.
1 <= t <= 105
Complexity Analysis
- Time Complexity: O(|\texttt{s}| + \log t)
- Space Complexity: O(26^2 + 26) = O(1)
3335. Total Characters in String After Transformations I LeetCode Solution in C++
class Solution {
public:
int lengthAfterTransformations(string s, int t) {
constexpr int kMod = 1'000'000'007;
vector<int> count(26);
for (const char c : s)
++count[c - 'a'];
while (t-- > 0) {
vector<int> newCount(26);
// 'a' -> 'b', 'b' -> 'c', ..., 'y' -> 'z'
for (int i = 0; i < 25; ++i)
newCount[i + 1] = count[i];
// 'z' -> 'ab'
newCount[0] = count[25];
newCount[1] = (newCount[1] + count[25]) % kMod;
count = std::move(newCount);
}
return accumulate(count.begin(), count.end(), 0L) % kMod;
}
};
/* code provided by PROGIEZ */
3335. Total Characters in String After Transformations I LeetCode Solution in Java
class Solution {
public int lengthAfterTransformations(String s, int t) {
final int kMod = 1_000_000_007;
int[] count = new int[26];
for (final char c : s.toCharArray())
++count[c - 'a'];
while (t-- > 0) {
int[] newCount = new int[26];
// 'a' -> 'b', 'b' -> 'c', ..., 'y' -> 'z'
for (int i = 0; i < 25; i++)
newCount[i + 1] = count[i];
// 'z' -> 'ab'
newCount[0] = count[25];
newCount[1] = (newCount[1] + count[25]) % kMod;
count = newCount;
}
return Arrays.stream(count).reduce(0, (a, b) -> (a + b) % kMod);
}
}
// code provided by PROGIEZ
3335. Total Characters in String After Transformations I LeetCode Solution in Python
class Solution:
def lengthAfterTransformations(self, s: str, t: int) -> int:
kMod = 1_000_000_007
count = [0] * 26
for c in s:
count[ord(c) - ord('a')] += 1
for _ in range(t):
newCount = [0] * 26
# 'a' -> 'b', 'b' -> 'c', ..., 'y' -> 'z'
for i in range(25):
newCount[i + 1] = count[i]
# 'z' -> 'ab'
newCount[0] = count[25]
newCount[1] = (newCount[1] + count[25]) % kMod
count = newCount
return sum(count) % kMod
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.