This commit is contained in:
parent
43527186ef
commit
362b79ae66
1 changed files with 15 additions and 0 deletions
15
medium/remove_all_occurrences_of_a_substring.py
Normal file
15
medium/remove_all_occurrences_of_a_substring.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# 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)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue