Total Characters in String After Transformations II Problem
Description
LeetCode Problem 3337.
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, ifs[i] = 'a'andnums[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, ifs[i] = 'y'andnums[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 exactly t transformations.
Since the answer may be very large, return it modulo 10^9 + 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 <= 10^5sconsists only of lowercase English letters.1 <= t <= 10^9nums.length == 261 <= nums[i] <= 25
Solution
Approach: Matrix exponentiation
As in the first version, only the 26 letter counts matter. Now letter i turns into the next nums[i] letters (cyclically), so one transformation is a linear map: build a 26x26 matrix M with M[i][(i + j) % 26] = 1 for j = 1..nums[i], and multiply the count vector by it.
Doing t rounds is multiplying by M^t, which fast exponentiation computes in O(log t) matrix products. Multiply the initial count vector by M^t, sum the entries and take everything modulo 10^9 + 7.
Complexity: O(n + 26^3 * log t) time and O(26^2) space.
class Solution:
def lengthAfterTransformations(self, s: str, t: int, nums: List[int]) -> int:
mod = 10**9 + 7
m = 26
cnt = [0] * m
for c in s:
cnt[ord(c) - ord("a")] += 1
matrix = [[0] * m for _ in range(m)]
for i, x in enumerate(nums):
for j in range(1, x + 1):
matrix[i][(i + j) % m] = 1
def matmul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
n, p, q = len(a), len(b), len(b[0])
res = [[0] * q for _ in range(n)]
for i in range(n):
for k in range(p):
if a[i][k]:
for j in range(q):
res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % mod
return res
def matpow(mat: List[List[int]], power: int) -> List[List[int]]:
res = [[int(i == j) for j in range(m)] for i in range(m)]
while power:
if power % 2:
res = matmul(res, mat)
mat = matmul(mat, mat)
power //= 2
return res
cnt = [cnt]
factor = matpow(matrix, t)
result = matmul(cnt, factor)[0]
ans = sum(result) % mod
return ans