This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
string findReplaceString(string S, vector<int>& indexes, vector<string>& sources, vector<string>& targets) { | |
int len = S.size(), n = indexes.size(); | |
vector<int> mapping(len, -1); | |
for (int i = 0; i < n; ++i) | |
{ | |
int subLen = sources[i].size(), idx = indexes[i]; | |
if (S.substr(idx, subLen) == sources[i])mapping[idx] = i; | |
} | |
int idx = 0; | |
string res; | |
while (idx < len) | |
{ | |
if (mapping[idx] != -1) | |
{ | |
int i = mapping[idx]; | |
res += targets[i]; | |
idx += sources[i].size(); | |
} | |
else | |
res += S[idx++]; | |
} | |
return res; | |
} | |
}; |
No comments:
Post a Comment