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