---
# System prepended metadata

title: 2381. Shifting Letters II
tags: [Leetcode, Line Sweep, Medium]

---

# 2381. Shifting Letters II
###### tags: `Leetcode` `Medium` `Line Sweep` 
Link: https://leetcode.com/problems/shifting-letters-ii/description/
## 思路
差分数组
注意diff的每一个值可能是很大的整数也可能是很小的负数 在加到原本的char之前 一定要把diff通过取余+26变成[0,25]之内的数才行
## Code
```java=
class Solution {
    public String shiftingLetters(String s, int[][] shifts) {
        int n = s.length();
        int[] diff = new int[n+1];
        for(int[] shift:shifts){
            int start = shift[0], end = shift[1], d = shift[2]==1?1:-1;
            diff[start] += d;
            diff[end+1] -= d;
        }
        for(int i=1; i<=n; i++) diff[i] += diff[i-1];
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<n; i++){
            char c = s.charAt(i);
            char newChar = (char)((c-'a'+diff[i]%26+26)%26+'a');
            sb.append(newChar);
        }
        return sb.toString();
    }
}
```