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.Problem Overview: Given two strings s and part, repeatedly remove the leftmost occurrence of part from s until the substring no longer appears. The result is the final string after all deletions. The main challenge is handling cascading removals where deleting one occurrence exposes another.
Approach 1: Iterative Removal using find and Slicing (Time: O(n*m*k), Space: O(n))
This method repeatedly searches for part inside s using a substring search operation such as find(). When a match is found, remove it by slicing the string: keep the prefix before the match and append the suffix after it. Continue the loop until find() returns -1. Each iteration scans the string again, and multiple removals can occur, so the worst-case complexity grows with the number of deletions. The approach is simple and readable, making it a good baseline solution when implementing quick string manipulation logic.
Approach 2: Stack-based String Reconstruction (Time: O(n*m), Space: O(n))
This approach builds the result incrementally using a stack-like structure. Iterate through each character in s and push it onto a stack (or append to a result string builder). After each insertion, check whether the last m characters match part. If they match, pop those characters from the stack. This simulates removing the substring immediately when it appears, preventing repeated full-string scans. The check is done only when the stack length is at least m, keeping the process efficient. This technique is a classic pattern in string processing problems where deletions may trigger new matches.
The stack reconstruction behaves like a streaming algorithm: characters are processed once, and removals happen locally at the stack top. Because of this, each character is pushed and popped at most once, giving near-linear behavior in practice. Many solutions implement the stack using a mutable string builder or array for faster append and pop operations.
Recommended for interviews: The stack-based solution is typically what interviewers expect. It demonstrates understanding of incremental processing and avoids repeated full scans of the string. Mentioning the simple find + slicing approach first shows you recognize the brute-force simulation, but implementing the stack-based reconstruction shows stronger algorithmic thinking and familiarity with simulation-style problems.
This approach involves iteratively searching for the 'part' inside the 's', and removing it using string slicing until no more occurrences are left.
This function uses strstr to find the first occurrence of part in s and memmove to remove it by shifting the remaining string.
Time Complexity: O(n * m) where n is the length of s and m is the length of part.
Space Complexity: O(1), in-place operations are performed.
In this approach, a stack is used to store characters of the resultant string while comparing with the part string. We check if the last few characters of the stack match the part string, and if so, roll back the stack by the length of part.
The solution builds the resultant string using a simulated stack within the original array, removing any matching part as detected.
Time Complexity: O(n * m) due to checking the last part length in each iteration.
Space Complexity: O(1), as it modifies the string in place.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Iterative Removal using `find` and Slicing | Time Complexity: O(n * m) where n is the length of |
| Stack-based String Reconstruction | Time Complexity: O(n * m) due to checking the last part length in each iteration. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative find and slicing | O(n*m*k) | O(n) | Simple implementation or quick prototype where readability matters more than performance |
| Stack-based string reconstruction | O(n*m) | O(n) | Preferred interview solution; avoids repeated full string scans and handles cascading removals efficiently |
Leetcode Solutions 5781. Remove All Occurrences of a Substring • Fraz • 17,785 views views
Watch 9 more video solutions →Practice Remove All Occurrences of a Substring with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor