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.
Function Signature
def remove_occurrences(s: str, part: str) -> str:
Rules
-
Each operation removes exactly one occurrence, the one that starts at the smallest index of the current string. A removal can join characters on both sides into a new occurrence, which a later operation then removes.
-
Stop when the current string no longer contains
part
. The result may be the empty string.
Constraints
-
1 <= len(s) <= 1000
-
1 <= len(part) <= 1000
-
s
and
part
consist of lowercase English letters.
Examples
Input: s = "daabcbaabcbc", part = "abc"
Output: "dab"
Removing "abc" at index 2 gives "dabaabcbc", removing it at index 4 gives "dababc", and removing it at index 3 gives "dab", which has no occurrence of "abc" left.
Input: s = "axxxxyyyyb", part = "xy"
Output: "ab"
The removals happen at indices 4, 3, 2 and 1, giving "axxxyyyb", "axxyyb", "axyb" and finally "ab". Each removal brings another x next to a y.
Input: s = "aabb", part = "ab"
Output: ""
Removing "ab" at index 1 leaves "ab", and removing that leaves the empty string.