3337. Total Characters in String After Transformations II
Difficulty: Hard
Topics: Hash Table
, Math
, String
, Dynamic Programming
, Counting
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 109 + 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'
asnums[0] == 1
-
'b'
becomes'c'
asnums[1] == 1
-
'c'
becomes'd'
asnums[2] == 1
-
'y'
becomes'z'
asnums[24] == 1
-
'y'
becomes'z'
asnums[24] == 1
- String after the first transformation:
"bcdzz"
-
-
Second Transformation (t = 2):
-
'b'
becomes'c'
asnums[1] == 1
-
'c'
becomes'd'
asnums[2] == 1
-
'd'
becomes'e'
asnums[3] == 1
-
'z'
becomes'ab'
asnums[25] == 2
-
'z'
becomes'ab'
asnums[25] == 2
- String after the second transformation:
"cdeabab"
-
-
Final Length of the string: The string is
"cdeabab"
, which has 7 characters.
-
First Transformation (t = 1):
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'
asnums[0] == 2
-
'z'
becomes'ab'
asnums[25] == 2
-
'b'
becomes'cd'
asnums[1] == 2
-
'k'
becomes'lm'
asnums[10] == 2
- String after the first transformation:
"bcabcdlm"
-
-
Final Length of the string: The string is
"bcabcdlm"
, which has 8 characters.
-
First Transformation (t = 1):
Constraints:
1 <= s.length <= 105
-
s
consists only of lowercase English letters. 1 <= t <= 109
nums.length == 26
1 <= nums[i] <= 25
Hint:
- Model the problem as a matrix multiplication problem.
- Use exponentiation to quickly multiply matrices.
Solution:
We need to determine the length of a string after applying a series of transformations. Each transformation replaces each character in the string with a specified number of consecutive characters, wrapping around the alphabet if necessary. Given the constraints, directly simulating each transformation is infeasible, so we use matrix exponentiation to efficiently compute the result after a large number of transformations.
Approach
- Matrix Representation: Represent each transformation step as a matrix where each entry (i, j) indicates how many times character j appears when character i is transformed.
- Matrix Exponentiation: Use matrix exponentiation to compute the effect of applying the transformation t times. This allows us to efficiently handle the large number of transformations (up to 1e9).
- Dot Product Calculation: For each character in the original string, compute its contribution to the final string length using the precomputed matrix and the given transformation counts.
Let's implement this solution in PHP: 3337. Total Characters in String After Transformations II
<?php
/**
* @param String $s
* @param Integer $t
* @param Integer[] $nums
* @return Integer
*/
function lengthAfterTransformations($s, $t, $nums) {
...
...
...
/**
* go to ./solution.php
*/
}
/**
* @param $nums
* @param $mod
* @return array
*/
function buildMatrix($nums, $mod) {
...
...
...
/**
* go to ./solution.php
*/
}
/**
* @param $a
* @param $b
* @param $mod
* @return array
*/
function matrix_multiply($a, $b, $mod) {
...
...
...
/**
* go to ./solution.php
*/
}
/**
* @param $mat
* @param $power
* @param $mod
* @return array
*/
function matrix_power($mat, $power, $mod) {
...
...
...
/**
* go to ./solution.php
*/
}
/**
* @return array
*/
function matrix_identity() {
...
...
...
/**
* go to ./solution.php
*/
}
// Example usage:
$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];
echo lengthAfterTransformations($s, $t, $nums); // Output: 7
$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];
echo lengthAfterTransformations($s, $t, $nums); // Output: 8
?>
Explanation:
-
Matrix Construction: The
buildMatrix
function constructs a 26x26 matrix where each entry (i, j) indicates how many times character j is generated when character i is transformed. -
Matrix Exponentiation: The
matrix_power
function computes the matrix raised to the power of (t-1) using exponentiation by squaring, which efficiently handles large exponents. -
Contribution Calculation: For each character in the original string, we compute its contribution to the final length by multiplying the corresponding row of the exponentiated matrix with the transformation counts array (
nums
), summing the results modulo 1e9+7.
This approach ensures that we efficiently compute the result even for very large values of t, leveraging matrix exponentiation to reduce the complexity from O(t) to O(log t).
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)