Repeatedly remove the leftmost occurrence of a substring from a string

Quick Overview

Repeatedly delete the leftmost occurrence of a pattern from a string until none remain, and return what is left. It tests single-pass string processing with a stack and reasoning about matches that only form after an earlier removal.

Repeatedly remove the leftmost occurrence of a substring from a string

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given two strings `s` and `part`, perform the following operation on `s` until all occurrences of the substring `part` are removed: - Find the leftmost occurrence of the substring `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. ### Function Signature ```python def remove_occurrences(s: str, part: str) -> str: ``` ### Rules - Each operation removes exactly one occurrence, the one that starts at the smallest index of the current string. A removal can join characters on both sides into a new occurrence, which a later operation then removes. - Stop when the current string no longer contains `part`. The result may be the empty string. ### Constraints - `1 <= len(s) <= 1000` - `1 <= len(part) <= 1000` - `s` and `part` consist of lowercase English letters. ### Examples ```text Input: s = "daabcbaabcbc", part = "abc" Output: "dab" ``` Removing `"abc"` at index 2 gives `"dabaabcbc"`, removing it at index 4 gives `"dababc"`, and removing it at index 3 gives `"dab"`, which has no occurrence of `"abc"` left. ```text Input: s = "axxxxyyyyb", part = "xy" Output: "ab" ``` The removals happen at indices 4, 3, 2 and 1, giving `"axxxyyyb"`, `"axxyyb"`, `"axyb"` and finally `"ab"`. Each removal brings another `x` next to a `y`. ```text Input: s = "aabb", part = "ab" Output: "" ``` Removing `"ab"` at index 1 leaves `"ab"`, and removing that leaves the empty string.

Overview: Repeatedly delete the leftmost occurrence of a pattern from a string until none remain, and return what is left. It tests single-pass string processing with a stack and reasoning about matches that only form after an earlier removal.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given two strings s and part, perform the following operation on s until all occurrences of the substring part are removed:

  • Find the leftmost occurrence of the substring 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.

Function Signature

def remove_occurrences(s: str, part: str) -> str:

Rules

  • Each operation removes exactly one occurrence, the one that starts at the smallest index of the current string. A removal can join characters on both sides into a new occurrence, which a later operation then removes.
  • Stop when the current string no longer contains part . The result may be the empty string.

Constraints

  • 1 <= len(s) <= 1000
  • 1 <= len(part) <= 1000
  • s and part consist of lowercase English letters.

Examples

Input: s = "daabcbaabcbc", part = "abc"
Output: "dab"

Removing "abc" at index 2 gives "dabaabcbc", removing it at index 4 gives "dababc", and removing it at index 3 gives "dab", which has no occurrence of "abc" left.

Input: s = "axxxxyyyyb", part = "xy"
Output: "ab"

The removals happen at indices 4, 3, 2 and 1, giving "axxxyyyb", "axxyyb", "axyb" and finally "ab". Each removal brings another x next to a y.

Input: s = "aabb", part = "ab"
Output: ""

Removing "ab" at index 1 leaves "ab", and removing that leaves the empty string.