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]));
//                }
//            }
//        }
        SourceTarget[] array = new SourceTarget[S.length()];
        for (int i = 0; i < indexes.length; i++) {
            array[indexes[i]] = new SourceTarget(sources[i], targets[i]);
        }

        StringBuilder sb = new StringBuilder();
        int i = 0;
        while (i < S.length()) {
            if (array[i] != null) {
                SourceTarget st = array[i];
                String sub = S.substring(i);
                if (sub.startsWith(st.source)) {
                    sb.append(st.target);
                    i += st.source.length();
                } else {
                    sb.append(S.charAt(i++));
                }
            } else {
                sb.append(S.charAt(i++));
            }
        }
        return sb.toString();
    }

    private static class SourceTarget {
        private final String source;
        private final String target;

        public SourceTarget(String source, String target) {
            this.source = source;
            this.target = target;
        }
    }

    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
    }
}

@@ -210,632 +210,8 @@
 ) %7B%0A
-//        StringBuilder sb = new StringBuilder();%0A//        for (int i = 0; i %3C indexes.length; i++) %7B%0A//            String sub = S.substring(indexes%5Bi%5D);%0A//            int index = sub.indexOf(sources%5Bi%5D);%0A//            if (index %3E= 0) %7B%0A//                sb.append(targets%5Bi%5D);%0A//            %7D%0A//            if (i + 1 %3C indexes.length) %7B%0A//                sb.append(S.substring(indexes%5Bi%5D + sources%5Bi%5D.length(), indexes%5Bi + 1%5D));%0A//            %7D else if (i + 1 == indexes.length) %7B%0A//                if (index %3C 0) %7B%0A//                    sb.append(S.substring(indexes%5Bi%5D));%0A//                %7D%0A//            %7D%0A//        %7D%0A
     
@@ -1239,888 +1239,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem833 prob = new Problem833();%0A        System.out.println(prob.findReplaceString(%22abcd%22, new int%5B%5D%7B0, 2%7D, new String%5B%5D%7B%22a%22, %22cd%22%7D,%0A            new String%5B%5D%7B%22eee%22, %22ffff%22%7D)); // eeebffff%0A        System.out.println(prob.findReplaceString(%22abcd%22, new int%5B%5D%7B0, 2%7D, new String%5B%5D%7B%22ab%22, %22ec%22%7D,%0A            new String%5B%5D%7B%22eee%22, %22ffff%22%7D)); // eeecd%0A        System.out.println(prob.findReplaceString(%22abcd%22, new int%5B%5D%7B0, 2%7D, new String%5B%5D%7B%22ab%22, %22fd%22%7D,%0A            new String%5B%5D%7B%22def%22, %22xxx%22%7D)); // defcd%0A        System.out.println(prob.findReplaceString(%22ab%22, new int%5B%5D%7B0, 1%7D, new String%5B%5D%7B%22a%22, %22b%22%7D,%0A            new String%5B%5D%7B%22xxx%22, %22yyy%22%7D)); // xxxyyy%0A        System.out.println(prob.findReplaceString(%22vmokgggqzp%22, new int%5B%5D%7B3, 5, 1%7D,%0A            new String%5B%5D%7B%22kg%22, %22ggq%22, %22mo%22%7D, new String%5B%5D%7B%22s%22, %22so%22, %22bfr%22%7D)); // vbfrssozp%0A    %7D
 %0A%7D%0A

Solve problem 833