Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
1.1 kB
0
Indexable
Never
class Solution {
public:
    string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {
        int n = indices.size();
        string ns = s;
        vector<pair<int,int>> v; 
        for(int i=0;i<n;i++) {
            v.push_back({indices[i],i});
        }
        sort(v.begin(),v.end());
        int shift = 0;
        for(int i=0;i<n;i++) {
            int j = v[i].second;
            int l = sources[j].size();
            // if(s.size()-v[i].first<l) continue;
            string t = s.substr(v[i].first+shift,l);
            // cout<<j<<" "<<sources[j]<<" "<<s<<" "<<t<<endl;
            if(t==sources[j]) {
                s = s.substr(0,v[i].first+shift) + targets[j] + s.substr(v[i].first+shift+l);
                shift += (targets[j].size() - sources[j].size());
            }
        }
        return s;
        // for(int i=0;i<n;i++) {
        //     int l = sources.size();
        //     if(s.size()-indices[i]<l) continue;
        //     string t = s.substring(indices[i],l);
        //     if(t==sources[i]) {
        //         ns = 
        //     }
        // }
    }
};
Leave a Comment