leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0833.cpp (966B)


      1 class Solution {
      2   public:
      3     string findReplaceString(const string &s, const vector<int> &indices, const vector<string> &sources,
      4                              const vector<string> &targets) const {
      5         const int n = size(indices);
      6 
      7         static int order[10001];
      8         iota(begin(order), begin(order) + n, 0);
      9         sort(begin(order), begin(order) + n, [&indices](int i, int j) { return indices[i] < indices[j]; });
     10 
     11         string res;
     12         int crnt = 0;
     13         for (int i = 0; i < n; i++) {
     14             const int idx = order[i], m = size(sources[idx]);
     15             int pos = 0;
     16 
     17             if (indices[idx] < crnt) continue;
     18             while (indices[idx] > crnt)
     19                 res += s[crnt++];
     20             while (pos < m && s[crnt + pos] == sources[idx][pos])
     21                 pos++;
     22             if (pos == m) res += targets[idx], crnt += m;
     23         }
     24         while (crnt < size(s))
     25             res += s[crnt++];
     26 
     27         return res;
     28     }
     29 };