forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShiftingLetters2.java
40 lines (39 loc) · 1.1 KB
/
ShiftingLetters2.java
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
/*https://leetcode.com/problems/shifting-letters-ii/*/
class Solution {
public String shiftingLetters(String s, int[][] shifts) {
char[] a = s.toCharArray();
int n = a.length, temp;
long q;
long[] changes = new long[n+1];
for (int[] shift : shifts)
{
if (shift[2] == 0)
{
changes[shift[0]] += -1;
changes[shift[1]+1] += 1;
}
else
{
changes[shift[0]] += 1;
changes[shift[1]+1] += -1;
}
}
for (int i = 1; i <= n; ++i)
{
changes[i] += changes[i-1];
q = changes[i-1]/26;
if (changes[i-1] <= -26)
changes[i-1] += 26*(-(q-1));
else if (changes[i-1] >= 26) changes[i-1] -= 26*q;
}
for (int i = 0; i < n; ++i)
{
temp = a[i]-'a';
temp += changes[i];
if (temp < 0) temp += 26;
if (temp > 25) temp -= 26;
a[i] = (char)(temp+'a');
}
return new String(a);
}
}