Find And Replace in String LeetCode solution in C++

1
class Solution {
2
public:
3
string findReplaceString(string s, vector<int> &indices, vector<string> &sources,
4
vector<string> &targets) {
5
int baggage = 0;
6
int n = indices.size();
7
map<int, pair<string, string>> mp;
8
for (int i = 0; i < n; ++i) {
9
mp[indices[i]] = {sources[i], targets[i]};
10
}
11
for (auto it : mp) {
12
string temp = s.substr(it.first + baggage, it.second.first.size());
13
if (temp == it.second.first) {
14
s.erase(it.first + baggage, it.second.first.size());
15
s.insert(it.first + baggage, it.second.second);
16
baggage += it.second.second.size() - it.second.first.size();
17
}
18
}
19
return s;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0