-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPerform String Shifts.cpp
51 lines (50 loc) · 1.16 KB
/
Perform String Shifts.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
42
43
44
45
46
47
48
49
50
51
class Solution {
public:
string stringShift(string s, vector<vector<int>>& shift) {
int left=0;
int right=0;
for(int i=0;i<shift.size();i++){
if(shift[i][0]==0){
left+=shift[i][1];
}
else if(shift[i][0]==1)
{
right+=shift[i][1];
}
}
int n=s.size();
int x;
string final="";
cout<<left<<" "<<right;
if(left>right){
x=left-right;
x=x%n;
string aage="";
for(int i=x;i<n;i++){
aage+=s[i];
}
string piche="";
for(int i=0;i<x;i++){
piche+=s[i];
}
final=aage + piche;
}
else {
x=right-left;
x=x%n;
string aage="";
for(int i=n-x;i<n;i++){
aage+=s[i];
}
string piche="";
for(int i=0;i<n-x;i++){
piche+=s[i];
}
final=aage + piche;
}
if(x==0){
return s;
}
return final;
}
};