Skip to content

Commit 37e18d4

Browse files
committed
0833 solved.
1 parent c27f16e commit 37e18d4

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
project(cpp_0833)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_0833 main.cpp)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// Source : https://leetcode.com/problems/find-and-replace-in-string/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-08-14
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <tuple>
8+
#include <algorithm>
9+
10+
using namespace std;
11+
12+
13+
class Solution {
14+
public:
15+
string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {
16+
17+
int n = indices.size();
18+
vector<tuple<int, string, string>> v(n);
19+
for(int i = 0; i < n; i ++) v[i] = make_tuple(indices[i], sources[i], targets[i]);
20+
sort(v.begin(), v.end());
21+
22+
int offset = 0;
23+
for(int i = 0; i < n; i ++){
24+
int index = get<0>(v[i]);
25+
string a = get<1>(v[i]), b = get<2>(v[i]);
26+
if(s.find(a, index + offset) == index + offset){
27+
s.replace(index + offset, a.size(), b);
28+
offset += b.size() - a.size();
29+
}
30+
}
31+
return s;
32+
}
33+
};
34+
35+
36+
int main() {
37+
38+
return 0;
39+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ email: [[email protected]](mailto:[email protected])
837837
| 830 | [Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/) | [solution](https://leetcode.com/problems/positions-of-large-groups/solution/) | [C++](0501-1000/0830-Positions-of-Large-Groups/cpp-0830/) | | |
838838
| 831 | [Masking Personal Information](https://leetcode.com/problems/masking-personal-information/) | [] | [C++](0501-1000/0831-Masking-Personal-Information/cpp-0831/) | | |
839839
| 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image/) | [solution](https://leetcode.com/problems/flipping-an-image/solution/) | [C++](0501-1000/0832-Flipping-an-Image/cpp-0832/) | | |
840-
| | | | | | |
840+
| 833 | [Find And Replace in String](https://leetcode.com/problems/find-and-replace-in-string/) | [] | [C++](https://leetcode.com/problems/find-and-replace-in-string/) | | |
841841
| 834 | [Sum of Distances in Tree](https://leetcode.com/problems/sum-of-distances-in-tree/) | [solution](https://leetcode.com/problems/sum-of-distances-in-tree/solution/) | [C++](0501-1000/0834-Sum-of-Distances-in-Tree/cpp-0834/) | | |
842842
| 835 | [Image Overlap](https://leetcode.com/problems/image-overlap/) | [] | [C++](0501-1000/0835-Image-Overlap/cpp-0835/) | | |
843843
| | | | | | |

0 commit comments

Comments
 (0)