-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
39 lines (35 loc) · 1.24 KB
/
Solution.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
#include <cstddef>
#include <cstdlib>
#include <string>
#include <vector>
class Solution {
public:
std::string shiftingLetters(std::string s,
std::vector<std::vector<int>>& shifts) {
// shifts[i] = [start, end, direction]
// shift the characters in s[start..end] inclusive, left (0) or right (1).
// Mhm, some form of intervals. Brute-force updates will result in O(nq).
// Probably need to accumulate the resultant shifts for each index i.
// Line-sweep/sorting would be useful.
constexpr int ALPHAS{26};
// Assign -1 to Left, +1 to Right for Start events.
// Negate for End events. We also want to +1 to the end idx, to denote
// inclusive ends.
//
// shifts = [[0,1,0],[1,2,1],[0,2,1]]
// events = [0, 1, 1, -2]
std::vector<int> events(s.size() + 1, 0);
for (const auto& shift : shifts) {
int direction = shift[2] == 0 ? -1 : 1;
events[shift[0]] += direction;
events[shift[1] + 1] -= direction;
}
int rightShifts = 0;
for (int i = 0; i < s.size(); ++i) {
rightShifts += events[i];
// Handle left shifts, i.e., negative modulos.
s[i] = ((((s[i] - 'a' + rightShifts) % ALPHAS) + ALPHAS) % ALPHAS) + 'a';
}
return s;
}
};