코딩 공부/Leetcode
[Leetcode / Python] 1910. Remove All Occurrences of a Substring
일하는 공학도
2025. 2. 11. 14:18
728x90
난이도 : medium
문자열 s의 part 문자열을 왼쪽 부터 하나씩 제거하는 문제.
맨 처음에는 그냥 import re 및 re.sub을 통해서 제거했으나, 한번에 제거하는 문제가 아니었다.
class Solution:
def removeOccurrences(self, s: str, part: str) -> str:
while part in s:
s = s.replace(part, "", 1)
return s
replace로 part를 빈 문자열로 바꾸고, 왼쪽부터 하나만 바꾼다는 while문으로 해결
Runtime : 0ms
Memory : 17.94MB
728x90