We have a string
Call the shift of a letter, the next letter in the alphabet, (wrapping around so that
For example,
Now for each
Return the final string after all such shifts to
Solutions in C++:
string shiftingLetters(string S, vector<int>& shifts) {
int sum = 0;
int iter = S.length() - 1;
for(auto it = shifts.rbegin(); it != shifts.rend(); ++it, --iter){
sum = sum + *it;
if(sum >= 26){
sum = sum % 26;
}
*it = sum;
S[iter] = (char)(int('a') + (S[iter] + *it - (int)('a')) % 26);
}
return S;
}
S of lowercase letters, and an integer array shifts.Call the shift of a letter, the next letter in the alphabet, (wrapping around so that
'z' becomes 'a').For example,
shift('a') = 'b', shift('t') = 'u', and shift('z') = 'a'.Now for each
shifts[i] = x, we want to shift the first i+1 letters of S, x times.Return the final string after all such shifts to
S are applied.Solutions in C++:
string shiftingLetters(string S, vector<int>& shifts) {
int sum = 0;
int iter = S.length() - 1;
for(auto it = shifts.rbegin(); it != shifts.rend(); ++it, --iter){
sum = sum + *it;
if(sum >= 26){
sum = sum % 26;
}
*it = sum;
S[iter] = (char)(int('a') + (S[iter] + *it - (int)('a')) % 26);
}
return S;
}
Comments
Post a Comment