Trains the technique from
LeetCode 1910Remove All Occurrences of a SubstringThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A label machine has printed a long paper tape carrying the lowercase letters of s, one letter per cell, and the operator has to strip a faulty marker part out of it.
The operator repeats one step for as long as the marker still appears anywhere on the tape:
part, that is the match beginning at the smallest cell index.Splicing can bring letters together that were far apart before, and the marker may well turn up across a fresh splice, in which case it is cut too. The operator stops only when no run of cells anywhere on the tape spells part.
Return the letters left on the tape when the operator stops, as a string. The result may be the empty string.
Example 1
The leftmost match sits at cells 1 and 2. Cutting it splices the remaining `a` and `b` together, and that pair spells the marker as well, so it goes too and nothing is left.
Example 2
The leftmost match is at the start. Cutting it leaves `pqqrpqr`, whose leftmost match is the final three cells, and cutting that leaves `pqqr`, which holds no match.
Example 3
No cell spells the marker, so the operator makes no cut and the tape is unchanged.
Example 4
Two runs spell the marker, one starting at cell 0 and one at cell 2, and they share cells. The operator takes the one starting at cell 0, leaving `ba`, which holds no match.
Example 5
The leftmost match covers cells 0 and 1, and cutting it leaves a single `a`.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def remove_occurrences(s: str, part: str) -> str:public String removeOccurrences(String s, String part)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.