MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / shiftingLetters

Method shiftingLetters

ShiftingLettersII.java:3–30  ·  view source on GitHub ↗
(String s, int[][] shifts)

Source from the content-addressed store, hash-verified

1
2class Solution {
3 public String shiftingLetters(String s, int[][] shifts) {
4 int n = s.length();
5 int[] arr = new int[n];
6 for (int[] shift : shifts) {
7 if (shift[2] == 1) { //fw
8 arr[shift[0]]++;
9 if (shift[1] + 1 < n) {
10 arr[shift[1] + 1]--;
11 }
12 } else { //bw
13 arr[shift[0]]--;
14 if (shift[1] + 1 < n) {
15 arr[shift[1] + 1]++;
16 }
17 }
18 }
19
20 StringBuilder result = new StringBuilder(s);
21 int sum = 0;
22 for (int i = 0; i < n; i++) {
23 sum = (sum + arr[i]) % 26;
24 if (sum < 0) sum += 26;
25 char shiftedChar = (char) ('a' +
26 ((s.charAt(i) - 'a' + sum) % 26));
27 result.setCharAt(i, shiftedChar);
28 }
29 return result.toString();
30 }
31}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected