---
# System prepended metadata

title: 833-Find And Replace in String
tags: [Medium]

---

# 833-Find And Replace in String
###### tags: `Medium`
## Question
https://leetcode.com/problems/find-and-replace-in-string/
## Key
1. 沒有用到特別的算法，重點在熟悉string STL，或是string、pair和vector之間的配合
2. vector.rbegin()/rend()、vector<pair<int,int>>、string.substr()、string.size()
## Reference
## Solution
```cpp=
class Solution {
public:
    string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {
    vector<pair<int, int>> vs;
    for(int i=0; i<indices.size();i++)
    {
        vs.push_back({indices[i], i});
    }
    sort(vs.rbegin(), vs.rend());
    for(auto v : vs)
    {
        
        int start = v.first;
        string com = sources[v.second];
        string t = targets[v.second];
        if(s.substr(start, com.size()) == com)
        {
            s = s.substr(0, start) + t + s.substr(start+ com.size());
        }
    }
    return s;     
    }
};
```

### 補充
https://crmne0707.pixnet.net/blog/post/316362030-c%2B%2B-%E5%AD%90%E5%AD%97%E4%B8%B2-substring

https://stackoverflow.com/questions/32208657/why-use-rbegin-instead-of-end-1