Problem
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
.
Examples
Example 1:
|
|
Example 2:
|
|
Constraints:
1 <= s.length <= 10^5
s
consists only of lowercase English letters.1 <= t <= 10^9
nums.length == 26
1 <= nums[i] <= 25
Solution
Method 1 - Using Matrix Exponentiation
The problem revolves around efficiently simulating a series of transformations on the occurrences of characters in the string s
. The naive approach of iterating through each transformation step is too slow, especially for large values of t
. Instead, we leverage the mathematical concept of matrix exponentiation to perform the transformations efficiently.
Note that:
- Character Transformations: For each transformation:
- A character
c
transforms into several other characters based onnums[c]
. - The transformations wrap around the 26 letters of the alphabet.
- A character
- Matrix Representation:
- We represent transformations using a transition matrix
T
, whereT[i][j] = 1
if characterj
can transform into characteri
, andT[i][j] = 0
otherwise. - The recurrence relation can be expressed as: $f(i) = T \times f(i-1)$, 1. - where
f(i)
is the column vector representing character frequencies at thei
-th step.
- We represent transformations using a transition matrix
- Efficiency with Matrix Exponentiation:
- Direct computation across
t
transformations is inefficient. Instead, we compute ( T^t ), thet
-th matrix power, using matrix exponentiation by squaring. - This reduces the time complexity from ( O(t \times n^3) ) to ( O(\log(t) \times n^3) ), where ( n = 26 ) (number of characters).
- Direct computation across
Approach
-
Matrix Construction:
- Build the transformation matrix
T
based onnums
. If characterj
transforms to characteri
, setT[i][j] = 1
.
- Build the transformation matrix
-
Matrix Exponentiation:
- Calculate
T^t
using exponentiation by squaring.
- Calculate
-
Initial Frequency Vector:
- Construct the frequency vector ( f(0) ), where
f(0)[i]
is the number of occurrences of characteri
in the input strings
.
- Construct the frequency vector ( f(0) ), where
-
Final Frequency Vector:
- Multiply the matrix
T^t
withf(0)
to computef(t)
, which represents the frequencies of all characters aftert
transformations.
- Multiply the matrix
-
Result Calculation:
- Sum the values in
f(t)
to obtain the total length of the transformed string.
- Sum the values in
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(log(t) * n^3)
- Matrix Construction:
O(n*n)
where ( n = 26 ) - Matrix Exponentiation:
O(log(t) * n^3)
- Matrix-Vector Multiplication:
O(n^2)
- Matrix Construction:
- 🧺 Space complexity:
O(n^2)
for storing matrix and vectors.