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.The problem asks to repeatedly remove the leftmost occurrence of a substring part
from a string s
until no more occurrences of part
exist. The solutions provided utilize a straightforward iterative approach. Let's break down the logic and complexity:
Approach:
The core idea is to repeatedly search for the substring part
within s
and remove it if found. This continues until part
is no longer present in s
. Different programming languages offer built-in functions to achieve this efficiently:
replaceFirst()
or replace(...,1)
: This method finds the first occurrence of part
and removes it. The 1
in the python replace
function limits replacement to the first occurence. If no occurrence is found, the string remains unchanged. This ensures that the leftmost occurrence is always targeted.
erase()
: (C++) Similar to replaceFirst()
, but it directly manipulates the string.
strings.Replace(s, part, "", 1)
: (Go) Again, this replaces the first occurrence only.
Code Explanation (Python Example):
class Solution:
def removeOccurrences(self, s: str, part: str) -> str:
while part in s:
s = s.replace(part, '', 1) #Removes the first occurrence of 'part'
return s
The Python code uses a while
loop that continues as long as part
is found within s
. Inside the loop, s.replace(part, "", 1)
replaces the first instance of part
with an empty string. The loop terminates when no more occurrences of part
are found.
The other languages follow a similar pattern, leveraging their respective string manipulation functions.
Time Complexity Analysis:
The time complexity is dominated by the repeated substring searches. In the worst-case scenario, the substring part
might be found at the beginning of the string repeatedly, leading to O(nmk) complexity, where:
s
part
However, in practice, the number of times the loop iterates is significantly less than n in most cases, making this solution efficient for many inputs. The replace
operation itself has an average time complexity of O(n) where n is the length of the string in many implementations.
Space Complexity Analysis:
The space complexity depends on how the string manipulations are implemented. In most cases, the space used is proportional to the length of the string, resulting in O(n) space complexity where n is the length of string s. The algorithm does not create significant auxiliary data structures.
Improvements (Optional):
For extremely large strings, a more optimized approach might involve using a more efficient string searching algorithm (like Knuth-Morris-Pratt or Boyer-Moore) to reduce the search time in the while
loop, although this is often not necessary unless dealing with very large strings. For average cases, the provided solutions are efficient.