-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShiftingLettersII.cpp
41 lines (33 loc) · 1.01 KB
/
ShiftingLettersII.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public:
string shiftingLetters(string s, vector<vector<int>>& shifts) {
int* cumm = new int[s.length()]{};
for (int i = 0; i < shifts.size(); i++) {
if (shifts[i][2] == 1) {
cumm[shifts[i][0]] += 1;
if (shifts[i][1]+1 < s.size()) {
cumm[shifts[i][1]+1] -= 1;
}
} else {
cumm[shifts[i][0]] -= 1;
if (shifts[i][1]+1 < s.size()) {
cumm[shifts[i][1]+1] += 1;
}
}
}
int sum = 0;
for (int i = 0; i < s.size(); i++) {
sum += cumm[i];
int temp = s[i]-'a';
temp += sum;
while(temp < 0) {
temp += 26;
}
while(temp >= 26) {
temp -= 26;
}
s[i] = 'a'+temp;
}
return s;
}
};