Skip to content

Commit

Permalink
Create 2381. Shifting Letters II (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Jan 5, 2025
2 parents 7f72963 + 2a664eb commit d5133ea
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions 2381. Shifting Letters II
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public:
string shiftingLetters(string s, vector<vector<int>>& shifts) {
int n=s.length();
vector<int> count(n+1,0);
for(auto it:shifts){
int l=it[0];
int r=it[1];
int dir=it[2];
if(dir == 1){
count[l]+=1;
count[r+1]-=1;
}else{
count[l]-=1;
count[r+1]+=1;
}
}
//for(int i=0;i<=n;i++) cout<<count[i]<<" ";
vector<int> pref(n+1,0);
pref[0]=count[0];
for(int i=1;i<=n;i++){
pref[i]=count[i]+pref[i-1];
}
//cout<<pref[n]<<endl;
for(int i=0;i<n;i++){
int ele=s[i]-'a';
if(pref[i] < 0){
ele=(ele + pref[i]) % 26;
//cout<<ele<<" ";
ele=(ele + 26) % 26;
cout<<ele<<endl;
s[i]=(ele+'a');
}else{
ele = (ele + pref[i]) % 26;
s[i]=(ele+'a');
}
}
return s;
}
};

0 comments on commit d5133ea

Please sign in to comment.