index array may not sorted
class Solution {
public String findReplaceString(String S, int[] indexes, String[] sources, String[] targets) {
StringBuilder sb = new StringBuilder();
int[] match = new int[S.length()];
Arrays.fill(match, -1);
for (int i = 0; i < sources.length; i++) {
String source = sources[i];
if (S.substring(indexes[i], indexes[i] + source.length()).equals(source)) {
match[indexes[i]] = i;
}
}
int i = 0;
while (i < S.length()) {
if (match[i] >= 0) {
sb.append(targets[match[i]]);
i += sources[match[i]].length();
} else {
sb.append(S.charAt(i));
i++;
}
}
return sb.toString();
}
}