-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0848-shifting-letters.js
40 lines (36 loc) · 1.15 KB
/
0848-shifting-letters.js
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
/**
* 848. Shifting Letters
* https://leetcode.com/problems/shifting-letters/
* Difficulty: Medium
*
* You are given a string s of lowercase English letters and an integer array shifts of the
* same length.
*
* Call the shift() of a letter, the next letter in the alphabet, (wrapping around so that
* 'z' becomes 'a').
*
* For example, shift('a') = 'b', shift('t') = 'u', and shift('z') = 'a'.
*
* Now for each shifts[i] = x, we want to shift the first i + 1 letters of s, x times.
*
* Return the final string after all such shifts to s are applied.
*/
/**
* @param {string} s
* @param {number[]} shifts
* @return {string}
*/
var shiftingLetters = function(s, shifts) {
const cumulativeShifts = new Array(s.length).fill(0);
cumulativeShifts[s.length - 1] = shifts[s.length - 1] % 26;
for (let i = s.length - 2; i >= 0; i--) {
cumulativeShifts[i] = (cumulativeShifts[i + 1] + shifts[i]) % 26;
}
let result = '';
for (let i = 0; i < s.length; i++) {
const charCode = s.charCodeAt(i);
const shiftedCode = ((charCode - 97 + cumulativeShifts[i]) % 26) + 97;
result += String.fromCharCode(shiftedCode);
}
return result;
};