Skip to content

Commit 81be90b

Browse files
committed
Add solution #3335
1 parent 44a71e2 commit 81be90b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-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,750 LeetCode solutions in JavaScript
1+
# 1,751 LeetCode solutions in JavaScript
22

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

@@ -1741,6 +1741,7 @@
17411741
3223|[Minimum Length of String After Operations](./solutions/3223-minimum-length-of-string-after-operations.js)|Medium|
17421742
3272|[Find the Count of Good Integers](./solutions/3272-find-the-count-of-good-integers.js)|Hard|
17431743
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|
1744+
3335|[Total Characters in String After Transformations I](./solutions/3335-total-characters-in-string-after-transformations-i.js)|Medium|
17441745
3341|[Find Minimum Time to Reach Last Room I](./solutions/3341-find-minimum-time-to-reach-last-room-i.js)|Medium|
17451746
3342|[Find Minimum Time to Reach Last Room II](./solutions/3342-find-minimum-time-to-reach-last-room-ii.js)|Medium|
17461747
3343|[Count Number of Balanced Permutations](./solutions/3343-count-number-of-balanced-permutations.js)|Hard|
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

Comments
 (0)