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 exactlyt transformations.
Since the answer may be very large, return it modulo10^9 + 7.
Input: s ="abcyy", t =2Output: 7Explanation:
***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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Input: s ="azbk", t =1Output: 5Explanation:
***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.
classSolution:
MOD: int =10**9+7# Define the modulo constantdeflength_after_transformations(self, s: str, t: int) -> int:
cnt = [0] *26# Array to count occurrences of each character# Initialize the count arrayfor ch in s:
cnt[ord(ch) - ord('a')] +=1# Perform t transformationsfor _ in range(t):
nxt = [0] *26# Temporary array to store new counts# 'z' transitions into 'a' and 'b' nxt[0] = cnt[25]
nxt[1] = (cnt[25] + cnt[0]) % self.MOD
# Characters 'a' to 'y' transition normallyfor i in range(2, 26):
nxt[i] = cnt[i -1]
cnt = nxt # Update counts# Sum up all counts modulo MODreturn sum(cnt) % self.MOD