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 part and remove it from s.

Return s after removing all occurrences of part.

substring is a contiguous sequence of characters in a string.

Idea

  • Repeatedly remove occurrences of part from s until none remain

Code

class Solution:
    def removeOccurrences(self, s: str, part: str) -> str:
        while s != (s:= s.replace(part, "", 1)):
            pass
 
        return s