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 <= 1000
1 <= part.length <= 1000
s
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:
The brute force approach for removing all occurrences of a smaller string from a larger string involves repeatedly searching for the smaller string within the larger string. If we find the smaller string, we remove it. We keep doing this until the smaller string can't be found anymore.
Here's how the algorithm would work step-by-step:
def remove_all_occurrences_brute_force(main_string, substring):
while True:
# Find the index of the substring
index = main_string.find(substring)
# If substring is not found, exit the loop
if index == -1:
break
# Remove the substring from the main string
main_string = main_string[:index] + main_string[index + len(substring):]
return main_string
We want to efficiently remove all instances of a specific smaller text (the substring) from a larger text (the main string). The trick is to carefully search for the substring and remove it whenever we find it, updating the main string as we go.
Here's how the algorithm would work step-by-step:
def remove_all_occurrences(main_string, substring_to_remove):
while True:
index_of_substring = main_string.find(substring_to_remove)
# Exit loop if substring is not found.
if index_of_substring == -1:
break
# Remove the substring by slicing the main string.
main_string = main_string[:index_of_substring] + main_string[index_of_substring + len(substring_to_remove):]
# Update main_string after removing a substring instance.
return main_string
Case | How to Handle |
---|---|
Empty string or substring | If either string or substring is empty, return the original string to avoid infinite loops or unexpected behavior. |
Substring longer than the main string | Return the original string immediately as the substring cannot be found. |
Substring not found in the main string | Return the original string as no replacements need to be made. |
Substring is the same as the main string | Return an empty string, indicating that the entire string was removed. |
Overlapping occurrences of the substring | The algorithm should correctly remove all overlapping occurrences by iterating and removing sequentially. |
Very long strings to check for performance | Consider using efficient string searching algorithms (e.g., KMP) to avoid quadratic time complexity. |
Main string consisting only of the substring | The result should be an empty string, which signifies that everything was removed. |
Null string or substring input | Throw an IllegalArgumentException or return null (depending on requirements) if either input is null. |