package leetcode;

/**
 * https://leetcode.com/problems/shifting-letters/
 */
public class Problem848 {
    public String shiftingLetters(String S, int[] shifts) {
        char[] chars = S.toCharArray();
        for (int i = 0; i < shifts.length; i++) {
            for (int j = 0; j <= i; j++) {
                int val = (chars[j] - 'a' + shifts[i]) % 26;
                chars[j] = (char) ('a' + val);
            }
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        Problem848 prob = new Problem848();
        System.out.println(prob.shiftingLetters("abc", new int[]{3, 5, 9})); // rpl
        System.out.println(prob.shiftingLetters("bad", new int[]{10, 20, 30})); // jyh
    }
}

@@ -209,54 +209,74 @@
     
-for (int i = 0; i %3C shifts.length; i++) %7B%0A    
+long totalSum = 0;%0A        long%5B%5D sums = new long%5Bshifts.length%5D;%0A
     
@@ -292,24 +292,35 @@
 int 
-j
+i
  = 0; 
-j %3C= i; j
+i %3C shifts.length; i
 ++) 
@@ -337,39 +337,63 @@
     
-    int val = (chars%5Bj%5D - 'a'
+sums%5Bi%5D = totalSum + shifts%5Bi%5D;%0A            totalSum
  +
+=
  shi
@@ -402,14 +402,8 @@
 s%5Bi%5D
-) %25 26
 ;%0A  
@@ -408,24 +408,26 @@
         
+%7D%0A
         
 chars%5Bj%5D
@@ -422,52 +422,109 @@
     
-chars%5Bj%5D = (char) ('a' + val);%0A            %7D
+for (int i = 0; i %3C shifts.length; i++) %7B%0A            long prevSum = 0;%0A            if (i - 1 %3E= 0) %7B
 %0A   
@@ -528,26 +528,24 @@
         
-%7D%0A
         
 return n
@@ -540,300 +540,200 @@
     
+p
 re
-turn new String(chars)
+vSum = sums%5Bi - 1%5D
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem848 prob = new Problem848();%0A        System.out.println(prob.shiftingLetters(%22abc%22, new int%5B%5D%7B3, 5, 9%7D)); // rpl%0A        System.out.println(prob.shiftingLetters(%22bad%22, new int%5B%5D%7B10, 20, 30%7D)); // jyh
+    %7D%0A            long val = (chars%5Bi%5D - 'a' + (totalSum - prevSum)) %25 26;%0A            chars%5Bi%5D = (char) ('a' + val);%0A        %7D%0A        return new String(chars);
 %0A   

Solve problem 848