forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
37 lines (27 loc) · 807 Bytes
/
main.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
/// Source : https://leetcode.com/problems/perform-string-shifts/
/// Author : liuyubobobo
/// Time : 2020-05-02
#include <iostream>
#include <vector>
using namespace std;
/// Mathematically to compute the net shifts
/// Time Complexity: O(|shift| + |s|)
/// Time Complexity: O(|s|)
class Solution {
public:
string stringShift(string s, vector<vector<int>>& shift) {
int right = 0;
for(const vector<int>& v: shift)
if(v[0]) right += v[1];
else right -= v[1];
if(right > 0){
int offset = right % s.size();
return s.substr(s.size() - offset) + s.substr(0, s.size() - offset);
}
int offset = (-right) % s.size();
return s.substr(offset) + s.substr(0, offset);
}
};
int main() {
return 0;
}