-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshifting_letters2.cpp
More file actions
74 lines (67 loc) · 1.82 KB
/
shifting_letters2.cpp
File metadata and controls
74 lines (67 loc) · 1.82 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* =====================================================================================
*
* Filename: shifting_letters2.cpp
*
* Description: 2381. Shifting Letters II
* https://leetcode.com/problems/shifting-letters-ii/
*
* Version: 1.0
* Created: 01/09/2025 21:14:02
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <string>
#include <tuple>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::string;
using std::tuple;
using std::vector;
// Difference array
class Solution {
public:
string shiftingLetters(string s, vector<vector<int>>& shifts) {
const int kIncrement = 1;
const int kDecrement = 0;
const int size = s.length();
vector<int> diffs(size, 0);
for (const auto& sh : shifts) {
if (sh[2] == kIncrement) {
diffs[sh[0]]++;
if ((sh[1] + 1) < size) {
diffs[sh[1] + 1]--;
}
} else if (sh[2] == kDecrement) {
diffs[sh[0]]--;
if ((sh[1] + 1) < size) {
diffs[sh[1] + 1]++;
}
}
}
int cnt = 0;
for (int i = 0; i < size; i++) {
cnt = (cnt + diffs[i]) % 26;
if (cnt < 0) {
cnt += 26;
}
s[i] = (s[i] - 'a' + cnt) % 26 + 'a';
}
return s;
}
};
TEST(Solution, shiftingLetters) {
vector<tuple<string, vector<vector<int>>, string>> cases = {
std::make_tuple("abc", vector<vector<int>>{{0, 1, 0}, {1, 2, 1}, {0, 2, 1}}, "ace"),
std::make_tuple("dztz", vector<vector<int>>{{0, 0, 0}, {1, 1, 1}}, "catz"),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().shiftingLetters(std::get<0>(c), std::get<1>(c)), std::get<2>(c));
}
}