Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 10a4ad8

Browse files
committed
Add solution #3337
1 parent ef07b06 commit 10a4ad8

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,765 LeetCode solutions in JavaScript
1+
# 1,766 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1756,6 +1756,7 @@
17561756
3272|[Find the Count of Good Integers](./solutions/3272-find-the-count-of-good-integers.js)|Hard|
17571757
3306|[Count of Substrings Containing Every Vowel and K Consonants II](./solutions/3306-count-of-substrings-containing-every-vowel-and-k-consonants-ii.js)|Medium|
17581758
3335|[Total Characters in String After Transformations I](./solutions/3335-total-characters-in-string-after-transformations-i.js)|Medium|
1759+
3337|[Total Characters in String After Transformations II](./solutions/3337-total-characters-in-string-after-transformations-ii.js)|Hard|
17591760
3341|[Find Minimum Time to Reach Last Room I](./solutions/3341-find-minimum-time-to-reach-last-room-i.js)|Medium|
17601761
3342|[Find Minimum Time to Reach Last Room II](./solutions/3342-find-minimum-time-to-reach-last-room-ii.js)|Medium|
17611762
3343|[Count Number of Balanced Permutations](./solutions/3343-count-number-of-balanced-permutations.js)|Hard|
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)