- categories: Code, Interview Question, leetcode, Medium
- source: https://leetcode.com/problems/remove-all-occurrences-of-a-substring
- topics: String Manipulation
Description
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
partand remove it froms.
Return s after removing all occurrences of part.
A substring is a contiguous sequence of characters in a string.
Idea
- Repeatedly remove occurrences of
partfromsuntil none remain
Code
class Solution:
def removeOccurrences(self, s: str, part: str) -> str:
while s != (s:= s.replace(part, "", 1)):
pass
return s