Input: s ="daabcbaabcbc", part ="abc"Output: "dab"**Explanation**: The following operations are done:- s ="da** _abc_** baabcbc", remove "abc" starting at index 2, so s ="dabaabcbc".- s ="daba** _abc_** bc", remove "abc" starting at index 4, so s ="dababc".- s ="dab** _abc_** ", 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 ="axxx** _xy_** yyyb", remove "xy" starting at index 4 so s ="axxxyyyb".- s ="axx** _xy_** yyb", remove "xy" starting at index 3 so s ="axxyyb".- s ="ax** _xy_** yb", remove "xy" starting at index 2 so s ="axyb".- s ="a** _xy_** b", 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.
This approach continues to remove the substring part from the string s until no more occurrences are found. It uses the contains method to check for the presence of part and the indexOf method to find its position. The substring method is used to rebuild the string without the part.
⏰ Time complexity: O((n/m) * n), where n is the length of the string s and m is the length of the substring part. In the worst case, each removal might take up to O(n) time, and we might have to do this n/m times.
🧺 Space complexity: O(n) due to the creation of new strings during each iteration.
Method 2 - Naive - Delete chars in mutable string#
To solve this problem, we can use a simple iterative approach:
We will keep looking for the part in string s.
As long as the part is found, we will remove it by reconstructing the string.
Once no more occurrences of part are found, we return the modified string.
This approach leverages the built-in string functions efficiently to find and modify the string iteratively.
classSolution {
public String removeOccurrences(String s, String part) {
StringBuilder ans =new StringBuilder(s);
int idx;
while ((idx = ans.indexOf(part)) !=-1) {
ans.delete(idx, idx + part.length());
}
return ans.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defremoveOccurrences(self, s: str, part: str) -> str:
ans = list(s)
m = len(part)
i =0while i <= len(ans) - m:
if"".join(ans[i:i + m]) == part:
ans = ans[:i] + s_list[i + part_len:]
i = max(0, i - m) # Reset index to check for overlapping partselse:
i +=1return"".join(ans)
Traverse the string from the beginning, pushing each character onto a stack until its size matches that of part.
When the stack’s size equals part, check characters from the end of part against the top of the stack. If they match, pop the stack for each matching character.
If a character in part does not match the top of the stack, refill the stack with the previously checked characters and continue.
After processing, build the final string using the remaining characters in the stack by converting them with StringBuilder or equivalent.
classSolution {
public String removeOccurrences(String s, String part) {
StringBuilder stack =new StringBuilder();
int m = part.length();
for (char c : s.toCharArray()) {
stack.append(c);
int len = stack.length();
if (len >= m && stack.substring(len - m).equals(part)) {
stack.delete(len - m, len);
}
}
return stack.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defremoveOccurrences(self, s: str, part: str) -> str:
stack: list[str] = []
m: int = len(part)
for ch in s:
stack.append(ch)
if len(stack) >= m and''.join(stack[-m:]) == part:
del stack[-m:]
return''.join(stack)