|
| 1 | +/** |
| 2 | + * 3337. Total Characters in String After Transformations II |
| 3 | + * https://leetcode.com/problems/total-characters-in-string-after-transformations-ii/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are given a string s consisting of lowercase English letters, an integer t representing |
| 7 | + * the number of transformations to perform, and an array nums of size 26. In one transformation, |
| 8 | + * every character in s is replaced according to the following rules: |
| 9 | + * - Replace s[i] with the next nums[s[i] - 'a'] consecutive characters in the alphabet. For |
| 10 | + * example, if s[i] = 'a' and nums[0] = 3, the character 'a' transforms into the next 3 |
| 11 | + * consecutive characters ahead of it, which results in "bcd". |
| 12 | + * - The transformation wraps around the alphabet if it exceeds 'z'. For example, if s[i] = 'y' |
| 13 | + * and nums[24] = 3, the character 'y' transforms into the next 3 consecutive characters ahead |
| 14 | + * of it, which results in "zab". |
| 15 | + * |
| 16 | + * Return the length of the resulting string after exactly t transformations. |
| 17 | + * |
| 18 | + * Since the answer may be very large, return it modulo 109 + 7. |
| 19 | + */ |
| 20 | + |
| 21 | +/** |
| 22 | + * @param {string} s |
| 23 | + * @param {number} t |
| 24 | + * @param {number[]} nums |
| 25 | + * @return {number} |
| 26 | + */ |
| 27 | +var lengthAfterTransformations = function(s, t, nums) { |
| 28 | + const MOD = 1000000007n; |
| 29 | + const freq = new Array(26).fill(0n); |
| 30 | + |
| 31 | + for (const char of s) { |
| 32 | + freq[char.charCodeAt(0) - 97]++; |
| 33 | + } |
| 34 | + |
| 35 | + const matrix = new Array(26).fill().map(() => new Array(26).fill(0n)); |
| 36 | + for (let i = 0; i < 26; i++) { |
| 37 | + for (let j = 0; j < nums[i]; j++) { |
| 38 | + matrix[(i + j + 1) % 26][i] = 1n; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + const finalMatrix = matrixPower(matrix, t); |
| 43 | + let total = 0n; |
| 44 | + |
| 45 | + for (let i = 0; i < 26; i++) { |
| 46 | + let sum = 0n; |
| 47 | + for (let j = 0; j < 26; j++) { |
| 48 | + sum = (sum + finalMatrix[i][j] * freq[j]) % MOD; |
| 49 | + } |
| 50 | + total = (total + sum) % MOD; |
| 51 | + } |
| 52 | + |
| 53 | + return Number(total); |
| 54 | + |
| 55 | + function matrixMultiply(a, b) { |
| 56 | + const result = new Array(26).fill().map(() => new Array(26).fill(0n)); |
| 57 | + for (let i = 0; i < 26; i++) { |
| 58 | + for (let j = 0; j < 26; j++) { |
| 59 | + for (let k = 0; k < 26; k++) { |
| 60 | + result[i][j] = (result[i][j] + a[i][k] * b[k][j]) % MOD; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + function matrixPower(mat, power) { |
| 68 | + let result = new Array(26).fill().map(() => new Array(26).fill(0n)); |
| 69 | + for (let i = 0; i < 26; i++) result[i][i] = 1n; |
| 70 | + |
| 71 | + while (power > 0) { |
| 72 | + if (power & 1) result = matrixMultiply(result, mat); |
| 73 | + mat = matrixMultiply(mat, mat); |
| 74 | + power >>= 1; |
| 75 | + } |
| 76 | + return result; |
| 77 | + } |
| 78 | +}; |
0 commit comments