# 848. Shifting Letters

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:
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. `1 <= S.length = shifts.length <= 20000`
2. `0 <= shifts[i] <= 10 ^ 9`

``````class Solution {
public:
string shiftingLetters(string S, vector<int>& shifts) {
for (int i = (int)shifts.size() - 2; i >= 0; --i) {
shifts[i] = (shifts[i] + shifts[i + 1]) % 26;
}
for (int i = 0; i < shifts.size(); ++i) {
S[i] = (S[i] - 'a' + shifts[i]) % 26 + 'a';
}
return S;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/848

https://leetcode.com/problems/shifting-letters/

https://leetcode.com/problems/shifting-letters/discuss/137906/C%2B%2BJavaPython-Easy-Understood

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation