diff options
author | Jack Sangdahl <[email protected]> | 2025-02-10 18:17:20 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-02-10 18:17:20 -0700 |
commit | ebefd1aafb68dcfec333ba24826884d029b40856 (patch) | |
tree | 7de07c7ffd729234c2b80f6213f51895ef3aa37b /remove_substring.py | |
parent | e1eadeafa56ea84d76a484de1c2dae912b9ec772 (diff) | |
download | leetcode-ebefd1aafb68dcfec333ba24826884d029b40856.tar.gz leetcode-ebefd1aafb68dcfec333ba24826884d029b40856.zip |
1910. remove all occurences of a substring
Diffstat (limited to 'remove_substring.py')
-rw-r--r-- | remove_substring.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/remove_substring.py b/remove_substring.py new file mode 100644 index 0000000..d273606 --- /dev/null +++ b/remove_substring.py @@ -0,0 +1,27 @@ +# 1910. 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 +""" + + +class Solution(object): + def removeOccurrences(self, s, part): + """ + :type s: str + :type part: str + :rtype: str + """ + while part in s: + s = s.replace(part, "", 1) + return s + + +if __name__ == "__main__": + obj = Solution() + print(obj.removeOccurrences(s="daabcbaabcbc", part="abc")) + print(obj.removeOccurrences(s="axxxxyyyyb", part="xy")) |