-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_occurrences.cpp
More file actions
54 lines (49 loc) · 1.35 KB
/
remove_occurrences.cpp
File metadata and controls
54 lines (49 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* =====================================================================================
*
* Filename: remove_occurrences.cpp
*
* Description: 1910. Remove All Occurrences of a Substring
* https://leetcode.com/problems/remove-all-occurrences-of-a-substring/
*
* Version: 1.0
* Created: 02/11/2025 11:06:21
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <string>
#include <tuple>
#include <vector>
#include "gtest/gtest.h"
using std::string;
using std::tuple;
using std::vector;
class Solution {
public:
string removeOccurrences(string s, string part) {
const int plen = part.length();
for (int i = plen - 1; i < s.length(); i++) {
if (i >= plen - 1) {
if (s.substr(i - plen + 1, plen) == part) {
s.erase(i - plen + 1, plen);
i = i - plen;
}
}
}
return s;
}
};
TEST(Solution, removeOccurrences) {
vector<tuple<string, string, string>> cases = {
std::make_tuple("daabcbaabcbc", "abc", "dab"),
std::make_tuple("axxxxyyyyb", "xy", "ab"),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().removeOccurrences(std::get<0>(c), std::get<1>(c)), std::get<2>(c));
}
}