[LeetCode] 1910. Remove All Occurrences of a Substring (Python)

2025. 2. 11. 12:46·코딩 테스트
728x90

Problem

 

Solution

string

part에 해당하는 문자가 s 에 존재하지 않을 때까지 제거하는 문제.

왜 replace를 생각하지 못했을까 ㅜㅜ

 

<다른 풀이>

class Solution:
    def removeOccurrences(self, s: str, part: str) -> str:
        while part in s:
            s = s.replace(part,"",1)
        return s
  • 시간복잡도 : O(N) , 최악의 경우 O(N^2)
  • 공간복잡도: O(N)

Code

class Solution:
    def removeOccurrences(self, s: str, part: str) -> str:
        idx = 0
        n = len(part)

        while(idx < len(s)):
            subStr = s[idx:idx+n]
            if (subStr == part):
                s = s[:idx] + s[idx+n:]
                #print(idx, s)
                idx = 0
            else:
                idx += 1

        return s

 

Complexity

Time Complexity

위와 마찬가지로 O(N), 최악의 경우 O(N^2)

 

Space Complexity

새로운 문자열이 반복 생성되므로 O(N)

728x90
저작자표시 비영리 변경금지 (새창열림)

'코딩 테스트' 카테고리의 다른 글

[LeetCode] 3066. Minimum Operations to Exceed Threshold Value II (Python)  (0) 2025.02.13
[LeetCode] 2342. Max Sum of a Pair With Equal Sum of Digits (Python)  (0) 2025.02.12
[LeetCode] 3174. Clear Digits (Python)  (0) 2025.02.10
[LeetCode] 2364. Count Number of Bad Pairs (Python)  (0) 2025.02.09
[LeetCode] 2349. Design a Number Container System (Python)  (0) 2025.02.08
'코딩 테스트' 카테고리의 다른 글
  • [LeetCode] 3066. Minimum Operations to Exceed Threshold Value II (Python)
  • [LeetCode] 2342. Max Sum of a Pair With Equal Sum of Digits (Python)
  • [LeetCode] 3174. Clear Digits (Python)
  • [LeetCode] 2364. Count Number of Bad Pairs (Python)
zoodi
zoodi
IT/개발 관련 지식을 기록하는 블로그입니다.
  • zoodi
    오늘의 기록
    zoodi
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 후기
        • 컨퍼런스
        • 일상리뷰
      • 금융경제
        • 뉴스
        • 금융IT용어
        • 경제 및 부동산
      • 코딩 테스트
      • 스터디
        • JAVA
        • Kotlin
        • Spring
        • React, Nextjs
        • 인공지능 AI
        • Cloud & k8s
        • Kafka
        • Database
        • Network
        • Algorithm
        • Hadoop
        • LINUX
        • R Programming
        • 기타 (소공, 보안)
      • 도서
      • 기타
  • 블로그 메뉴

    • 홈
    • 스터디
    • 금융경제
    • 후기
    • 기타
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Python
    LeetCode
    네트워크
    쿠버네티스
    프로그래머스
    db
    리트코드
    카카오코테
    스프링
    알고리즘
    자바
    코테공부
    java
    자료구조
    Kotlin
    Spring
    pythoncodingtest
    코테
    스프링부트
    이분탐색
    kafka
    CodingTest
    codility
    코딩테스트
    코딜리티
    MySQL
    코딩
    금융용어
    springboot
    C++
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
zoodi
[LeetCode] 1910. Remove All Occurrences of a Substring (Python)
상단으로

티스토리툴바