Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 * 104shifts[i].length == 30 <= starti <= endi < s.length0 <= directioni <= 1s consists of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
"abc" [[0,1,0],[1,2,1],[0,2,1]]
"dztz" [[0,0,0],[1,1,1]]
the-skyline-problem)range-sum-query-mutable)range-addition)shifting-letters)maximum-population-year)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2381: Shifting Letters II
class Solution {
public String shiftingLetters(String s, int[][] shifts) {
int n = s.length();
int[] d = new int[n + 1];
for (int[] e : shifts) {
if (e[2] == 0) {
e[2]--;
}
d[e[0]] += e[2];
d[e[1] + 1] -= e[2];
}
for (int i = 1; i <= n; ++i) {
d[i] += d[i - 1];
}
StringBuilder ans = new StringBuilder();
for (int i = 0; i < n; ++i) {
int j = (s.charAt(i) - 'a' + d[i] % 26 + 26) % 26;
ans.append((char) ('a' + j));
}
return ans.toString();
}
}
// Accepted solution for LeetCode #2381: Shifting Letters II
func shiftingLetters(s string, shifts [][]int) string {
n := len(s)
d := make([]int, n+1)
for _, e := range shifts {
if e[2] == 0 {
e[2]--
}
d[e[0]] += e[2]
d[e[1]+1] -= e[2]
}
for i := 1; i <= n; i++ {
d[i] += d[i-1]
}
ans := []byte{}
for i, c := range s {
j := (int(c-'a') + d[i]%26 + 26) % 26
ans = append(ans, byte('a'+j))
}
return string(ans)
}
# Accepted solution for LeetCode #2381: Shifting Letters II
class Solution:
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str:
n = len(s)
d = [0] * (n + 1)
for i, j, v in shifts:
if v == 0:
v = -1
d[i] += v
d[j + 1] -= v
for i in range(1, n + 1):
d[i] += d[i - 1]
return ''.join(
chr(ord('a') + (ord(s[i]) - ord('a') + d[i] + 26) % 26) for i in range(n)
)
// Accepted solution for LeetCode #2381: Shifting Letters II
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2381: Shifting Letters II
// class Solution {
// public String shiftingLetters(String s, int[][] shifts) {
// int n = s.length();
// int[] d = new int[n + 1];
// for (int[] e : shifts) {
// if (e[2] == 0) {
// e[2]--;
// }
// d[e[0]] += e[2];
// d[e[1] + 1] -= e[2];
// }
// for (int i = 1; i <= n; ++i) {
// d[i] += d[i - 1];
// }
// StringBuilder ans = new StringBuilder();
// for (int i = 0; i < n; ++i) {
// int j = (s.charAt(i) - 'a' + d[i] % 26 + 26) % 26;
// ans.append((char) ('a' + j));
// }
// return ans.toString();
// }
// }
// Accepted solution for LeetCode #2381: Shifting Letters II
function shiftingLetters(s: string, shifts: number[][]): string {
const n: number = s.length;
const d: number[] = new Array(n + 1).fill(0);
for (let [i, j, v] of shifts) {
if (v === 0) {
v--;
}
d[i] += v;
d[j + 1] -= v;
}
for (let i = 1; i <= n; ++i) {
d[i] += d[i - 1];
}
let ans: string = '';
for (let i = 0; i < n; ++i) {
const j = (s.charCodeAt(i) - 'a'.charCodeAt(0) + (d[i] % 26) + 26) % 26;
ans += String.fromCharCode('a'.charCodeAt(0) + j);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.