https://leetcode.com/problems/shifting-letters/
https://leetcode.com/problems/shifting-letters/discuss/137906/C%2B%2BJavaPython-Easy-Understood
X.
https://leetcode.com/problems/shifting-letters/discuss/138128/Java-clean-solution
We have a string
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.
Example 1:
Input: S = "abc", shifts = [3,5,9] Output: "rpl" Explanation: We start with "abc". After shifting the first 1 letters of S by 3, we have "dbc". After shifting the first 2 letters of S by 5, we have "igc". After shifting the first 3 letters of S by 9, we have "rpl", the answer.
Note:
1 <= S.length = shifts.length <= 20000
0 <= shifts[i] <= 10 ^ 9
https://leetcode.com/problems/shifting-letters/discuss/137906/C%2B%2BJavaPython-Easy-Understood
One pass to count suffix sum of
One pass to shift letters in string
You can combine 2 passes as I did in 2-lines C++ version.
shifts
.One pass to shift letters in string
S
You can combine 2 passes as I did in 2-lines C++ version.
public String shiftingLetters(String S, int[] shifts) {
StringBuilder res = new StringBuilder(S);
for (int i = shifts.length - 2; i >= 0; i--) shifts[i] = (shifts[i] + shifts[i + 1]) % 26;
for (int i = 0; i < S.length(); i++) res.setCharAt(i, (char)((S.charAt(i) - 'a' + shifts[i]) % 26 + 'a'));
return res.toString();
}
The
i
th character is shifted shifts[i] + shifts[i+1] + ... + shifts[shifts.length - 1]
times. That's because only operations at the i
th operation and after, affect the i
th character.
Let
X
be the number of times the current i
th character is shifted. Then the next character i+1
is shifted X - shifts[i]
times.
For example, if
S.length = 4
and S[0]
is shifted X = shifts[0] + shifts[1] + shifts[2] + shifts[3]
times, then S[1]
is shifted shifts[1] + shifts[2] + shifts[3]
times, S[2]
is shifted shifts[2] + shifts[3]
times, and so on.
In general, we need to do
X -= shifts[i]
to maintain the correct value of X
as we increment i
.
public String shiftingLetters(String S, int[] shifts) {
StringBuilder ans = new StringBuilder();
int X = 0;
for (int shift : shifts)
X = (X + shift) % 26;
for (int i = 0; i < S.length(); ++i) {
int index = S.charAt(i) - 'a';
ans.append((char) ((index + X) % 26 + 97));
X = Math.floorMod(X - shifts[i], 26);
}
return ans.toString();
}
X.
https://leetcode.com/problems/shifting-letters/discuss/138128/Java-clean-solution
public String shiftingLetters(String S, int[] shifts) {
char[] arr = S.toCharArray();
int shift = 0;
for (int i = arr.length - 1; i >= 0; i--) {
shift = (shift + shifts[i]) % 26;
arr[i] = (char)((arr[i] - 'a' + shift) % 26 + 'a');
}
return new String(arr);
}
string shiftingLetters(string S, vector<int> shifts) {
for (int i = S.length() - 1; i >= 0; i--) S[i] = (S[i] - 'a' + (sf[i] = i + 1 < S.length() ? (sf[i] + sf[i + 1]) % 26 : sf[i])) % 26 + 'a';
return S;
}