Skip to main content
Back to problems
Leetcode
Medium
Strings
Stacks
Remove All Occurrences Of A Substring

Repeatedly remove every occurrence of a given substring from a string until no occurrence remains.

Acceptance 0%
Problem Statement

Given a string s and a string part, repeatedly find and remove an occurrence of part from s until part no longer appears in the string.

Return the final string after all such removals.

The removals may happen in any order of occurrence, but the end result must reflect continuing removals until the substring disappears completely.

Input Format

  • Two strings are given: s and part.
  • part is the substring to remove from s.
  • Treat the input as plain lowercase/ASCII strings unless otherwise specified by the platform.

Output Format

  • Return a single string: the result after removing all occurrences of part repeatedly.

Constraints

  • 1 <= s.length
  • 1 <= part.length <= s.length
  • The exact official platform limits are not provided here.
  • A correct solution should be efficient enough for linear or near-linear processing of the string.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "daabcbaabcbc", part = "abc"

Output

"dab"

Explanation

Remove "abc" from "daabcbaabcbc" -> "dabaabcbc". Remove "abc" again -> "dabcbc". Remove "abc" again -> "dab".

Example 2

Input

s = "axxxxyyyyb", part = "xy"

Output

"ab"

Explanation

After repeatedly removing "xy" occurrences, only "ab" remains.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.