You are given a 0-indexed string s that you must perform k replacement operations on. The replacement operations are given as three 0-indexed parallel arrays, indices, sources, and targets, all of length k.
To complete the ith replacement operation:
Check if the substringsources[i] occurs at index indices[i] in the original strings.
If it does not occur, do nothing.
Otherwise if it does occur, replace that substring with targets[i].
For example, if s = "_ab_ cd", indices[i] = 0, sources[i] = "ab", and
targets[i] = "eee", then the result of this replacement will be "_eee_ cd".
All replacement operations must occur simultaneously , meaning the replacement operations should not affect the indexing of each other. The testcases will be generated such that the replacements will not overlap.
For example, a testcase with s = "abc", indices = [0, 1], and sources = ["ab","bc"] will not be generated because the "ab" and "bc" replacements overlap.
Return _theresulting string after performing all replacement operations on _s.
A substring is a contiguous sequence of characters in a string.

Input: s ="abcd", indices =[0,2], sources =["a","cd"], targets =["eee","ffff"]Output: "eeebffff"Explanation:
"a" occurs at index 0in s, so we replace it with"eee"."cd" occurs at index 2in s, so we replace it with"ffff".

Input: s ="abcd", indices =[0,2], sources =["ab","ec"], targets =["eee","ffff"]Output: "eeecd"Explanation:
"ab" occurs at index 0in s, so we replace it with"eee"."ec" does not occur at index 2in s, so we do nothing.
We want to perform multiple replacements in a string, but replacements can overlap or affect each other. To avoid issues, we process the replacements in order of increasing index, and build the result string by checking at each index if a replacement should occur.
Pair up each replacement as (index, source, target) and sort by index.
Iterate through the string s with a pointer i.
For each replacement, if the current index matches and the source matches the substring, append the target to the result and skip the length of the source.
Otherwise, append the current character and move to the next index.
classSolution {
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
int n = s.length(), k = indices.length;
int[] match =newint[n];
Arrays.fill(match, -1);
for (int i = 0; i < k; i++) {
if (s.startsWith(sources[i], indices[i])) match[indices[i]]= i;
}
StringBuilder ans =new StringBuilder();
for (int i = 0; i < n; ) {
if (match[i]>= 0) {
ans.append(targets[match[i]]);
i += sources[match[i]].length();
} else {
ans.append(s.charAt(i++));
}
}
return ans.toString();
}
}
classSolution {
funfindReplaceString(s: String, indices: IntArray, sources: Array<String>, targets: Array<String>): String {
val n = s.length
val match = IntArray(n) { -1 }
for (i in indices.indices) {
if (s.startsWith(sources[i], indices[i])) match[indices[i]] = i
}
val ans = StringBuilder()
var i = 0while (i < n) {
if (match[i] >=0) {
ans.append(targets[match[i]])
i += sources[match[i]].length
} else {
ans.append(s[i])
i++ }
}
return ans.toString()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
deffindReplaceString(self, s: str, indices: list[int], sources: list[str], targets: list[str]) -> str:
n = len(s)
match= [-1] * n
for i, idx in enumerate(indices):
if s.startswith(sources[i], idx):
match[idx] = i
ans = []
i =0while i < n:
ifmatch[i] >=0:
ans.append(targets[match[i]])
i += len(sources[match[i]])
else:
ans.append(s[i])
i +=1return''.join(ans)
impl Solution {
pubfnfind_replace_string(s: String, indices: Vec<i32>, sources: Vec<String>, targets: Vec<String>) -> String {
let n = s.len();
letmut match_idx =vec![-1; n];
let s_bytes = s.as_bytes();
for (i, &idx) in indices.iter().enumerate() {
let idx = idx asusize;
let src = sources[i].as_bytes();
if idx + src.len() <= n &&&s_bytes[idx..idx+src.len()] == src {
match_idx[idx] = i asi32;
}
}
letmut ans = String::new();
letmut i =0;
while i < n {
if match_idx[i] >=0 {
let k = match_idx[i] asusize;
ans.push_str(&targets[k]);
i += sources[k].len();
} else {
ans.push(s_bytes[i] aschar);
i +=1;
}
}
ans
}
}
⏰ Time complexity: O(n + k * m), where n is the length of s, k is the number of replacements, and m is the average length of sources. Each replacement is checked and applied in linear time.
🧺 Space complexity: O(n + k), for the match array and result string.