Skip to content

Commit

Permalink
shifting letters
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherali Obidov committed Jul 28, 2018
1 parent 9bb9a21 commit 03a5897
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/problems/Medium.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2800,4 +2800,28 @@ Explanation: Alice's hand can't be rearranged into groups of 4.

1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
1 <= W <= hand.length
190)Shifting Letters
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
22 changes: 22 additions & 0 deletions src/problems/medium/ShiftingLetters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package problems.medium;

/**
* Why Did you create this class? what does it do?
*/
public class ShiftingLetters {
public String shiftingLetters(String s, int[] a) {
if (a == null || a.length == 0)
return s;
a[a.length - 1] %= 26;
for (int i = a.length - 2; i >= 0; i--) {
a[i] = (a[i] + a[i + 1]) % 26;
}
char[] c = s.toCharArray();
for (int i = 0; i < a.length; i++) {
c[i] = (char) (a[i] + c[i]);
if (c[i] > 'z')
c[i] = (char) (c[i] % 'z' + 'a' - 1);
}
return new String(c);
}
}

0 comments on commit 03a5897

Please sign in to comment.