Given two strings s and part, perform the following operation on s until all occurrences of the substring part are removed:
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:
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:
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 <= 10001 <= part.length <= 1000s and part consists of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a long string of text and you want to completely erase a specific smaller word or pattern hidden inside it. The brute force approach repeatedly scans the main text from start to finish, looking for that hidden pattern, and chops it out whenever it appears.
Here's how the algorithm would work step-by-step:
def remove_occurrences(main_string, target_pattern):
# We repeatedly loop because removing a pattern can create new instances of the pattern
while target_pattern in main_string:
pattern_index = main_string.find(target_pattern)
# Slicing is necessary to cleanly excise the target while preserving the rest of the text
main_string = main_string[:pattern_index] + main_string[pattern_index + len(target_pattern):]
return main_stringTo remove all copies of a specific target word from a main message, we can process the text from start to finish while constantly checking our work. Whenever the target word accidentally forms at the end of what we have processed so far, we simply wipe it away before moving forward.
Here's how the algorithm would work step-by-step:
def remove_occurrences(main_message: str, target_word: str) -> str:
running_collection = []
target_length = len(target_word)
for current_letter in main_message:
running_collection.append(current_letter)
# We check the end of our collection to catch the forbidden word immediately.
if len(running_collection) >= target_length and ''.join(running_collection[-target_length:]) == target_word:
# Deleting the matched suffix prevents cascading instances from being missed.
del running_collection[-target_length:]
# Joining the remaining letters ensures no hidden copies of the target word remain.
return ''.join(running_collection)| Case | How to Handle |
|---|---|
| The part string is longer than the source string s | The algorithm naturally finds no match and returns the original string unchanged. |
| The source string s is empty | The loop condition fails immediately and returns an empty string. |
| Removing a part creates a new instance of part in the resulting string | Repeatedly searching from the beginning or using a stack-based approach handles cascading removals correctly. |
| The part string is identical to the source string s | The entire string is removed in one pass, leaving an empty result. |
| Maximum-sized inputs with many overlapping or repeating patterns | Using a stack or StringBuilder instead of repeated string concatenation avoids quadratic time complexity. |
| The part string does not appear in s at all | The search operation returns not found on the first attempt and returns s as-is. |