-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2381. Shifting Letters II.java
More file actions
74 lines (58 loc) · 2.88 KB
/
2381. Shifting Letters II.java
File metadata and controls
74 lines (58 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
2381. Shifting Letters II
Solved
Medium
Topics
Companies
Hint
You are given a string s of lowercase English letters and a 2D integer array shifts where shifts[i] = [starti, endi, directioni]. For every i, shift the characters in s from the index starti to the index endi (inclusive) forward if directioni = 1, or shift the characters backward if directioni = 0.
Shifting a character forward means replacing it with the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Similarly, shifting a character backward means replacing it with the previous letter in the alphabet (wrapping around so that 'a' becomes 'z').
Return the final string after all such shifts to s are applied.
Example 1:
Input: s = "abc", shifts = [[0,1,0],[1,2,1],[0,2,1]]
Output: "ace"
Explanation: Firstly, shift the characters from index 0 to index 1 backward. Now s = "zac".
Secondly, shift the characters from index 1 to index 2 forward. Now s = "zbd".
Finally, shift the characters from index 0 to index 2 forward. Now s = "ace".
Example 2:
Input: s = "dztz", shifts = [[0,0,0],[1,1,1]]
Output: "catz"
Explanation: Firstly, shift the characters from index 0 to index 0 backward. Now s = "cztz".
Finally, shift the characters from index 1 to index 1 forward. Now s = "catz".
Constraints:
1 <= s.length, shifts.length <= 5 * 104
shifts[i].length == 3
0 <= starti <= endi < s.length
0 <= directioni <= 1
s consists of lowercase English letters.
class Solution {
public String shiftingLetters(String s, int[][] shifts) {
int n = s.length();
int[] diffArray = new int[n]; // Initialize a difference array with all elements set to 0.
// Process each shift operation
for (int[] shift : shifts) {
if (shift[2] == 1) { // If direction is forward (1)
diffArray[shift[0]]++; // Increment at the start index
if (shift[1] + 1 < n) {
diffArray[shift[1] + 1]--; // Decrement at the end+1 index
}
} else { // If direction is backward (0)
diffArray[shift[0]]--; // Decrement at the start index
if (shift[1] + 1 < n) {
diffArray[shift[1] + 1]++; // Increment at the end+1 index
}
}
}
StringBuilder result = new StringBuilder(s);
int numberOfShifts = 0;
// Apply the shifts to the string
for (int i = 0; i < n; i++) {
numberOfShifts = (numberOfShifts + diffArray[i]) % 26; // Update cumulative shifts, keeping within the alphabet range
if (numberOfShifts < 0) numberOfShifts += 26; // Ensure non-negative shifts
// Calculate the new character by shifting `s[i]`
char shiftedChar = (char) ('a' +
((s.charAt(i) - 'a' + numberOfShifts) % 26));
result.setCharAt(i, shiftedChar);
}
return result.toString();
}
}