-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42_removeOccurrences.py
More file actions
21 lines (21 loc) · 901 Bytes
/
42_removeOccurrences.py
File metadata and controls
21 lines (21 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'''Remove All Occurrences of a Substring
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.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: s = "daabcbaabcbc", part = "abc"
Output: "dab"
Explanation: The following operations are done:
- s = "daabcbaabcbc", remove "abc" starting at index 2, so s = "dabaabcbc".
- s = "dabaabcbc", remove "abc" starting at index 4, so s = "dababc".
- s = "dababc", remove "abc" starting at index 3, so s = "dab".
Now s has no occurrences of "abc".
'''
class Solution:
def removeOccurrences(self, s: str, part: str) -> str:
if len(s)<len(part):return s
while part in s:
s=s.replace(part,'',1)
return s