Skip to content

Latest commit

 

History

History
45 lines (33 loc) · 1.22 KB

find-and-replace-in-string.md

File metadata and controls

45 lines (33 loc) · 1.22 KB

Solution

    class Solution {
    public:
        string findReplaceString(string s, vector<int>& indexes, vector<string>& sources, vector<string>& targets) {
            
            string ans = "";
            vector<pair<int, int>> p;
            
            for(int i = 0; i < indexes.size(); i++) {
                p.push_back(make_pair(indexes[i], i));
            }
            
            sort(p.begin(), p.end());
            int j = 0;
            
            for(int i = 0; i < s.length(); i++) {
                if(j >= p.size() || i != p[j].first)
                    ans += s[i];
                else {
                    int idx = s.find(sources[p[j].second], p[j].first);
                    if(idx != string::npos && idx == p[j].first) {
                        ans += targets[p[j].second];
                        i += sources[p[j].second].length() - 1;
                    }
                    else
                        ans += s[i];
                    j++;
                }
            }
            return ans;
        }
    };