leetpycode/medium/remove_all_occurrences_of_a_substring.py

15 lines
483 B
Python

# https://leetcode.com/problems/remove-all-occurrences-of-a-substring
class Solution:
def removeOccurrences(self, s: str, part: str) -> str:
result = []
def is_part():
return "".join(result[-len(part):]) == part
for ch in s:
result.append(ch)
if len(result) >= len(part):
if is_part():
for _ in range(len(part)):
result.pop()
return "".join(result)