class Solution {
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
List<Pair<Integer, Integer>> sortedIndices = new ArrayList<>();
for (int i = 0; i < indices.length; ++i)
sortedIndices.add(new Pair<>(indices[i], i));
Collections.sort(sortedIndices, Comparator.comparing(Pair::getKey, Comparator.reverseOrder()));
for (Pair<Integer, Integer> sortedIndex : sortedIndices) {
final int index = sortedIndex.getKey();
final int i = sortedIndex.getValue();
final String source = sources[i];
final String target = targets[i];
if (index + source.length() <= s.length() &&
s.substring(index, index + source.length()).equals(source))
s = s.substring(0, index) + target + s.substring(index + source.length());
}
return s;
}
}