package leetcode;

/**
 * https://leetcode.com/problems/find-and-replace-in-string/
 */
public class Problem833 {
    public String findReplaceString(String S, int[] indexes, String[] sources, String[] targets) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < indexes.length; i++) {
            String sub = S.substring(indexes[i]);
            int index = sub.indexOf(sources[i]);
            if (index >= 0) {
                sb.append(targets[i]);
            }
            if (i + 1 < indexes.length) {
                sb.append(S.substring(indexes[i] + sources[i].length(), indexes[i + 1]));
            } else if (i + 1 == indexes.length) {
                if (index < 0) {
                    sb.append(S.substring(indexes[i]));
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Problem833 prob = new Problem833();
//        System.out.println(prob.findReplaceString("abcd", new int[]{0, 2}, new String[]{"a", "cd"},
//            new String[]{"eee", "ffff"})); // eeebffff
//        System.out.println(prob.findReplaceString("abcd", new int[]{0, 2}, new String[]{"ab", "ec"},
//            new String[]{"eee", "ffff"})); // eeecd
//        System.out.println(prob.findReplaceString("abcd", new int[]{0, 2}, new String[]{"ab", "fd"},
//            new String[]{"def", "xxx"})); // defcd
//        System.out.println(prob.findReplaceString("ab", new int[]{0, 1}, new String[]{"a", "b"},
//            new String[]{"xxx", "yyy"})); // xxxyyy
        System.out.println(prob.findReplaceString("vmokgggqzp", new int[]{3, 5, 1},
            new String[]{"kg", "ggq", "mo"}, new String[]{"s", "so", "bfr"})); // vbfrssozp
    }
}

@@ -202,24 +202,26 @@
  targets) %7B%0A
+//
         Stri
@@ -252,24 +252,26 @@
 gBuilder();%0A
+//
         for 
@@ -305,24 +305,26 @@
 gth; i++) %7B%0A
+//
             
@@ -353,32 +353,34 @@
 ng(indexes%5Bi%5D);%0A
+//
             int 
@@ -408,24 +408,26 @@
 ources%5Bi%5D);%0A
+//
             
@@ -440,24 +440,26 @@
 dex %3E= 0) %7B%0A
+//
             
@@ -485,16 +485,18 @@
 ts%5Bi%5D);%0A
+//
         
@@ -493,32 +493,34 @@
 //            %7D%0A
+//
             if (
@@ -537,32 +537,34 @@
 dexes.length) %7B%0A
+//
                 
@@ -633,24 +633,26 @@
 s%5Bi + 1%5D));%0A
+//
             
@@ -685,24 +685,26 @@
 s.length) %7B%0A
+//
             
@@ -720,24 +720,26 @@
 ndex %3C 0) %7B%0A
+//
             
@@ -782,16 +782,18 @@
 s%5Bi%5D));%0A
+//
         
@@ -794,32 +794,34 @@
               %7D%0A
+//
             %7D%0A  
@@ -810,32 +810,34 @@
 //            %7D%0A
+//
         %7D%0A      
@@ -842,29 +842,1023 @@
     
-return sb.toString();
+SourceTarget%5B%5D array = new SourceTarget%5BS.length()%5D;%0A        for (int i = 0; i %3C indexes.length; i++) %7B%0A            array%5Bindexes%5Bi%5D%5D = new SourceTarget(sources%5Bi%5D, targets%5Bi%5D);%0A        %7D%0A%0A        StringBuilder sb = new StringBuilder();%0A        int i = 0;%0A        while (i %3C S.length()) %7B%0A            if (array%5Bi%5D != null) %7B%0A                SourceTarget st = array%5Bi%5D;%0A                String sub = S.substring(i);%0A                if (sub.startsWith(st.source)) %7B%0A                    sb.append(st.target);%0A                    i += st.source.length();%0A                %7D else %7B%0A                    sb.append(S.charAt(i++));%0A                %7D%0A            %7D else %7B%0A                sb.append(S.charAt(i++));%0A            %7D%0A        %7D%0A        return sb.toString();%0A    %7D%0A%0A    private static class SourceTarget %7B%0A        private final String source;%0A        private final String target;%0A%0A        public SourceTarget(String source, String target) %7B%0A            this.source = source;%0A            this.target = target;%0A        %7D
 %0A   
@@ -1946,26 +1946,24 @@
 oblem833();%0A
-//
         Syst
@@ -2046,26 +2046,24 @@
 %22a%22, %22cd%22%7D,%0A
-//
             
@@ -2105,18 +2105,16 @@
 eebffff%0A
-//
         
@@ -2202,26 +2202,24 @@
 ab%22, %22ec%22%7D,%0A
-//
             
@@ -2258,18 +2258,16 @@
 / eeecd%0A
-//
         
@@ -2355,26 +2355,24 @@
 ab%22, %22fd%22%7D,%0A
-//
             
@@ -2410,18 +2410,16 @@
 / defcd%0A
-//
         
@@ -2507,18 +2507,16 @@
 , %22b%22%7D,%0A
-//
         

Update problem 833