Leetcode 2021 Solution

This article provides solution to leetcode question 2021 (remove-all-occurrences-of-a-substring)

https://leetcode.com/problems/remove-all-occurrences-of-a-substring

Solution

class Solution:
    def removeOccurrences(self, s: str, part: str) -> str:
        ans = ""

        for ch in s:
            ans += ch

            if len(ans) >= len(part) and ans[-len(part):] == part:
                ans = ans[:-len(part)]
        return ans