You are given a string s consisting of lowercase English letters, an integer t representing the number of transformations to perform, and an array nums of size 26. In one transformation, every character in s is replaced according to the following rules:
Replace s[i] with the nextnums[s[i] - 'a'] consecutive characters in the alphabet. For example, if s[i] = 'a' and nums[0] = 3, the character 'a' transforms into the next 3 consecutive characters ahead of it, which results in "bcd".
The transformation wraps around the alphabet if it exceeds 'z'. For example, if s[i] = 'y' and nums[24] = 3, the character 'y' transforms into the next 3 consecutive characters ahead of it, which results in "zab".
Return the length of the resulting string after exactlyt transformations.
Since the answer may be very large, return it modulo109 + 7.
Example 1:
Input:s = "abcyy", t = 2, nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2]
Output:7
Explanation:
First Transformation (t = 1):
'a' becomes 'b' as nums[0] == 1
'b' becomes 'c' as nums[1] == 1
'c' becomes 'd' as nums[2] == 1
'y' becomes 'z' as nums[24] == 1
'y' becomes 'z' as nums[24] == 1
String after the first transformation: "bcdzz"
Second Transformation (t = 2):
'b' becomes 'c' as nums[1] == 1
'c' becomes 'd' as nums[2] == 1
'd' becomes 'e' as nums[3] == 1
'z' becomes 'ab' as nums[25] == 2
'z' becomes 'ab' as nums[25] == 2
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, nums = [2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
Output:8
Explanation:
First Transformation (t = 1):
'a' becomes 'bc' as nums[0] == 2
'z' becomes 'ab' as nums[25] == 2
'b' becomes 'cd' as nums[1] == 2
'k' becomes 'lm' as nums[10] == 2
String after the first transformation: "bcabcdlm"
Final Length of the string: The string is "bcabcdlm", which has 8 characters.
Constraints:
1 <= s.length <= 105
s consists only of lowercase English letters.
1 <= t <= 109
nums.length == 26
1 <= nums[i] <= 25
Solutions
Solution 1: Fast Matrix Exponentiation to Accelerate Recurrence
We define \(f[i][j]\) as the number of times the \(j\)-th letter appears in the alphabet after \(i\) transformations. Initially, \(f[0][j]\) corresponds to the frequency of the \(j\)-th letter in the input string \(s\).
Since the frequency of each letter after a transformation affects the next transformation, and the total number of transformations \(t\) can be large, we can accelerate this recurrence process using fast matrix exponentiation.
Note that the result can be very large, so we take modulo \(10^9 + 7\).
The time complexity of this approach is \(O(n + \log t \times |\Sigma|^3)\), where \(n\) is the length of the string and \(|\Sigma|\) is the size of the alphabet (in this case, 26). The space complexity is \(O(|\Sigma|^2)\), which is the size of the matrix used for matrix multiplication.