Skip to content

Latest commit

 

History

History
52 lines (39 loc) · 1.55 KB

_1910. Remove All Occurrences of a Substring.md

File metadata and controls

52 lines (39 loc) · 1.55 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : February 11, 2025

Last updated : February 11, 2025


Related Topics : String, Stack, Simulation

Acceptance Rate : 78.0 %


Solutions

Python

class Solution:
    def removeOccurrences(self, s: str, part: str) -> str:
        left, right = list(reversed(s)), []

        while left :
            curr = left.pop()
            right.append(curr)
            if len(left) >= len(part) - 1 and curr == part[0] :
                matches = True
                for i in range(len(part) - 1) :
                    right.append(left.pop())
                    if right[-1] != part[i + 1] :
                        matches = False
                        for j in range(i + 1) :
                            left.append(right.pop())
                        break
                    
                if matches :
                    for i in range(len(part)) :
                        right.pop()
                    for i in range(min(len(part), len(right))) :
                        left.append(right.pop())

        return ''.join(right)