Halo

A magic place for coding

0%

LeetCode解题报告(488)-- 1910. Remove All Occurrences of a Substring

Problem

Given two strings s and part, perform the following operation on s until all occurrences of the substring part are removed:

  • Find the leftmost occurrence of the substring part and remove it from s.

Return s after removing all occurrences of part.

A substring is a contiguous sequence of characters in a string.

Example 1:

1
2
3
4
5
6
7
Input: s = "daabcbaabcbc", part = "abc"
Output: "dab"
Explanation: The following operations are done:
- s = "daabcbaabcbc", remove "abc" starting at index 2, so s = "dabaabcbc".
- s = "dabaabcbc", remove "abc" starting at index 4, so s = "dababc".
- s = "dababc", remove "abc" starting at index 3, so s = "dab".
Now s has no occurrences of "abc".

Example 2:

1
2
3
4
5
6
7
8
Input: s = "axxxxyyyyb", part = "xy"
Output: "ab"
Explanation: The following operations are done:
- s = "axxxxyyyyb", remove "xy" starting at index 4 so s = "axxxyyyb".
- s = "axxxyyyb", remove "xy" starting at index 3 so s = "axxyyb".
- s = "axxyyb", remove "xy" starting at index 2 so s = "axyb".
- s = "axyb", remove "xy" starting at index 1 so s = "ab".
Now s has no occurrences of "xy".

Constraints:

  • 1 <= s.length <= 1000
  • 1 <= part.length <= 1000
  • s and part consists of lowercase English letters.

Analysis

  这道题目给出一个字符串s以及一个模式part,要求当字符串存在子串和part一样时,就消除掉,问最后剩下的部分是什么。这道题目消除连续k个相同的字符串是一个道理,有两个下标实现一个类似stack的逻辑。下标i为快下标,用来遍历s,另一个下标j为慢下标,用来维护剩余的字符串到哪里了。每次检查substr(j - m, m)是否和part相等,mpart的长度,如果相等则回退j。最后j指向的位置就是结果的位置。

Solution

  无。


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
string removeOccurrences(string s, string part) {
string x = s;
int n = s.size(), m = part.size(), j = 0;
for (int i = 0; i < n; ++i) {
x[j++] = s[i];
if (j >= m && x.substr(j - m, m) == part) {
j -= m;
}
}
return x.substr(0, j);
}
};

Summary

  这道题目和之前消除连续k个字符原理是一样的,只不过这道题目消除的是子串,消除的逻辑不同。之前的题目是通过统计数字来消除,而这道题目是通过对比子串消除。。这道题目的分享到这里,感谢你的支持!

Welcome to my other publishing channels