-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0833-find-and-replace-in-string.js
58 lines (52 loc) · 1.99 KB
/
0833-find-and-replace-in-string.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 833. Find And Replace in String
* https://leetcode.com/problems/find-and-replace-in-string/
* Difficulty: Medium
*
* 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:
* 1. Check if the substring sources[i] occurs at index indices[i] in the original string s.
* 2. If it does not occur, do nothing.
* 3. Otherwise if it does occur, replace that substring with targets[i].
*
* For example, if s = "abcd", indices[i] = 0, sources[i] = "ab", and targets[i] = "eee", then
* the result of this replacement will be "eeecd".
*
* 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 the resulting string after performing all replacement operations on s.
*
* A substring is a contiguous sequence of characters in a string.
*/
/**
* @param {string} s
* @param {number[]} indices
* @param {string[]} sources
* @param {string[]} targets
* @return {string}
*/
var findReplaceString = function(s, indices, sources, targets) {
const replacements = [];
for (let i = 0; i < indices.length; i++) {
const index = indices[i];
const source = sources[i];
const target = targets[i];
if (s.substring(index, index + source.length) === source) {
replacements.push({ index, source, target });
}
}
replacements.sort((a, b) => b.index - a.index);
let result = s;
for (const { index, source, target } of replacements) {
result = result.substring(0, index) + target + result.substring(index + source.length);
}
return result;
};