Ctrl K

Erase the Pattern Until It Is Gone

Medium
You get strings text and pattern. While pattern occurs in text as a substring (a contiguous piece), find its **leftmost** occurrence and cut it out, joining the parts on either side. Return what remains once there are no occurrences left.
Example 1
text="kcatcatt", pattern="cat" → 'kt'

Removing "cat" at index 1 gives "kcatt"; removing "cat" again leaves "kt".

Example 2
text="ooxxoo", pattern="ox" → 'oo'

"ooxxoo" → "oxoo" → "oo".

Example 3
text="abc", pattern="abcd" → 'abc'

The pattern is longer than the text, so nothing is removed.

Constraints
  • 1 ≤ len(text) ≤ 1000
  • 1 ≤ len(pattern) ≤ 1000
  • Both strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCrashes on One Lane MediumStar as Backspace MediumMirror Letter Pairing Score MediumSame Text After Erasing EasyCollapse Letter Pairs Easy
loading Python…
Input
text="kcatcatt", pattern="cat"
Expected
'kt'

Run checks the first 3 tests in your browser. Submit checks all 132 tests on the server, hidden ones included.