We have a string S
of lowercase letters, and an integer array shifts
.
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.
Example 1:
Input: S = "abc", shifts = [3,5,9] Output: "rpl" Explanation: We start with "abc". After shifting the first 1 letters of S by 3, we have "dbc". After shifting the first 2 letters of S by 5, we have "igc". After shifting the first 3 letters of S by 9, we have "rpl", the answer.
Note:
1 <= S.length = shifts.length <= 20000
0 <= shifts[i] <= 10 ^ 9
因为不知道把数字转成字符的函数,所以就开了两个对象来进行帮助转换,思路就是,把每个字符的总变换次数求出来,然后挨个求解
/**
* @param {string} S
* @param {number[]} shifts
* @return {string}
*/
var shiftingLetters = function(S, shifts) {
var tot2 = [];
var fu ={
0:'a',
1:'b',
2:'c',
3:'d',
4:'e',
5:'f',
6:'g',
7:'h',
8:'i',
9:'j',
10:'k',
11:'l',
12:'m',
13:'n',
14:'o',
15:'p',
16:'q',
17:'r',
18:'s',
19:'t',
20:'u',
21:'v',
22:'w',
23:'x',
24:'y',
25:'z'
}
var index = {
'a':0,
'b':1,
'c':2,
'd':3,
'e':4,
'f':5,
'g':6,
'h':7,
'i':8,
'j':9,
'k':10,
'l':11,
'm':12,
'n':13,
'o':14,
'p':15,
'q':16,
'r':17,
's':18,
't':19,
'u':20,
'v':21,
'w':22,
'x':23,
'y':24,
'z':25
}
var len = shifts.length;
var num = 0;
var tot = [];
for(let i = 0; i < len; i ++){
if(i == 0)
tot2[i] = shifts[i];
else tot2[i] = tot2[i - 1] + shifts[i];
num += shifts[i];
}
for(let i = 0; i < len; i ++){
if(i == 0) tot[i] = num;
else tot[i] = num - tot2[i - 1];
}
console.log(tot);
var len2 = S.length;
var ans = '';
for(let i = 0; i < len2; i ++){
ans += fu[ (index[S[i] ] + tot[i] % 26) % 26 ];
//console.log(ans);
}
return ans;
};