-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3335-total-characters-in-string-after-transformations-i.js
More file actions
47 lines (42 loc) · 1.35 KB
/
3335-total-characters-in-string-after-transformations-i.js
File metadata and controls
47 lines (42 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 3335. Total Characters in String After Transformations I
* https://leetcode.com/problems/total-characters-in-string-after-transformations-i/
* Difficulty: Medium
*
* You are given a string s and an integer t, representing the number of transformations to
* perform. In one transformation, every character in s is replaced according to the following
* rules:
* - If the character is 'z', replace it with the string "ab".
* - Otherwise, replace it with the next character in the alphabet. For example, 'a' is replaced
* with 'b', 'b' is replaced with 'c', and so on.
*
* Return the length of the resulting string after exactly t transformations.
*
* Since the answer may be very large, return it modulo 109 + 7.
*/
/**
* @param {string} s
* @param {number} t
* @return {number}
*/
var lengthAfterTransformations = function(s, t) {
const MOD = 1e9 + 7;
const freq = new Array(26).fill(0);
for (const char of s) {
freq[char.charCodeAt(0) - 97]++;
}
for (let i = 0; i < t; i++) {
const newFreq = new Array(26).fill(0);
for (let j = 0; j < 25; j++) {
newFreq[j + 1] = freq[j];
}
newFreq[0] = (newFreq[0] + freq[25]) % MOD;
newFreq[1] = (newFreq[1] + freq[25]) % MOD;
freq.splice(0, 26, ...newFreq);
}
let result = 0;
for (const count of freq) {
result = (result + count) % MOD;
}
return result;
};