[LeetCode] Find And Replace in String

833. Find And Replace in String

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.

  • new solution update 2022.02.25
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
class Solution {
int fenwick[1001]{0,};
void update(int index, int value) {
index += 1;
while(index < 1002) {
fenwick[index] += value;
index += (index & -index);
}
}
int qry(int index) {
index += 1; //zero prevention
int res = 0;
while(index > 0) {
res += fenwick[index];
index -= (index & -index);
}
return res;
}

public:
string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {
int n = indices.size();
for(int i = 0; i < n; i++) {
int index = indices[i] + qry(indices[i]);
bool check = index + sources[i].length() <= s.length();
for(int j = index; j < index + sources[i].length() and check; j++) {
if(s[j] != sources[i][j-index]) check = false;
}
if(check) {
s.replace(index, sources[i].length(), targets[i]);
update(indices[i], targets[i].length() - sources[i].length());
}
}
return s;
}
};
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
class Solution {
bool check(string& s, int st, string& target) {
int i = st, j = 0;
for(; i < s.length() and j < target.length(); i++, j++) {
if(s[i] != target[j]) return false;
}
return i - st == j;
}
int getAdditionalIndexSum(map<int, int>& additionalIndex, int i) {
int res = 0;
for(auto it = additionalIndex.begin(); it != additionalIndex.end() and it->first <= i; it++) {
res += it->second;
}
return res;
}
public:
string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {
map<int, int> additionalIndex;
int n = indices.size();
for(int i = 0; i < n; i++) {
int startIndex = indices[i] + getAdditionalIndexSum(additionalIndex, indices[i]);
if(check(s, startIndex, sources[i])) {
s = s.replace(startIndex, sources[i].length(), targets[i]);
additionalIndex[indices[i]] += -sources[i].length() + targets[i].length();
}
}
return s;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/13/PS/LeetCode/find-and-replace-in-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.