Skip to content

Commit c9ec3f9

Browse files
authored
Create perform-string-shifts.cpp
1 parent 274439d commit c9ec3f9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

C++/perform-string-shifts.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n + l)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string stringShift(string s, vector<vector<int>>& shift) {
7+
int left_shift = 0;
8+
for (const auto& x : shift) {
9+
if (!x[0]) {
10+
left_shift += x[1];
11+
} else {
12+
left_shift -= x[1];
13+
}
14+
}
15+
left_shift = ((left_shift) % int(s.length()) + int(s.length())) % int(s.length());
16+
reverse(begin(s), begin(s) + left_shift);
17+
reverse(begin(s) + left_shift, end(s));
18+
reverse(begin(s), end(s));
19+
return s;
20+
}
21+
};

0 commit comments

Comments
 (0)