-
Notifications
You must be signed in to change notification settings - Fork 0
/
848-shifting-letters.cpp
26 lines (23 loc) · 1.1 KB
/
848-shifting-letters.cpp
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
// Title: Shifting Letters
// Description:
// 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.
// Link: https://leetcode.com/problems/shifting-letters/
// Time complexity: O(n)
// Space complexity: O(n)
class Solution {
public:
string shiftingLetters(string s, vector<int>& shifts) {
/* naive solution will be rejected by TLE! */
// we need to accumulate the shifts on the same letter and then perform the shift at once
int accumulatedShift = 0;
for (int i = shifts.size() - 1; i >= 0; i--) {
accumulatedShift = (accumulatedShift + shifts[i]) % 26;
s[i] = (s[i] - 'a' + accumulatedShift) % 26 + 'a';
}
return s;
}
};