Skip to content

Commit 8239bb5

Browse files
committed
Sync LeetCode submission Runtime - 16 ms (18.84%), Memory - 18 MB (32.79%)
1 parent b2ff1fa commit 8239bb5

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p>Given two strings <code>s</code> and <code>part</code>, perform the following operation on <code>s</code> until <strong>all</strong> occurrences of the substring <code>part</code> are removed:</p>
2+
3+
<ul>
4+
<li>Find the <strong>leftmost</strong> occurrence of the substring <code>part</code> and <strong>remove</strong> it from <code>s</code>.</li>
5+
</ul>
6+
7+
<p>Return <code>s</code><em> after removing all occurrences of </em><code>part</code>.</p>
8+
9+
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> s = &quot;daabcbaabcbc&quot;, part = &quot;abc&quot;
16+
<strong>Output:</strong> &quot;dab&quot;
17+
<strong>Explanation</strong>: The following operations are done:
18+
- s = &quot;da<strong><u>abc</u></strong>baabcbc&quot;, remove &quot;abc&quot; starting at index 2, so s = &quot;dabaabcbc&quot;.
19+
- s = &quot;daba<strong><u>abc</u></strong>bc&quot;, remove &quot;abc&quot; starting at index 4, so s = &quot;dababc&quot;.
20+
- s = &quot;dab<strong><u>abc</u></strong>&quot;, remove &quot;abc&quot; starting at index 3, so s = &quot;dab&quot;.
21+
Now s has no occurrences of &quot;abc&quot;.
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> s = &quot;axxxxyyyyb&quot;, part = &quot;xy&quot;
28+
<strong>Output:</strong> &quot;ab&quot;
29+
<strong>Explanation</strong>: The following operations are done:
30+
- s = &quot;axxx<strong><u>xy</u></strong>yyyb&quot;, remove &quot;xy&quot; starting at index 4 so s = &quot;axxxyyyb&quot;.
31+
- s = &quot;axx<strong><u>xy</u></strong>yyb&quot;, remove &quot;xy&quot; starting at index 3 so s = &quot;axxyyb&quot;.
32+
- s = &quot;ax<strong><u>xy</u></strong>yb&quot;, remove &quot;xy&quot; starting at index 2 so s = &quot;axyb&quot;.
33+
- s = &quot;a<strong><u>xy</u></strong>b&quot;, remove &quot;xy&quot; starting at index 1 so s = &quot;ab&quot;.
34+
Now s has no occurrences of &quot;xy&quot;.
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
42+
<li><code>1 &lt;= part.length &lt;= 1000</code></li>
43+
<li><code>s</code>​​​​​​ and <code>part</code> consists of lowercase English letters.</li>
44+
</ul>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Approach 2: Stack
2+
3+
# Time: O(n * m)
4+
# Space: O(n + m)
5+
6+
class Solution:
7+
def removeOccurrences(self, s: str, part: str) -> str:
8+
stack = []
9+
part_length = len(part)
10+
11+
for char in s:
12+
stack.append(char)
13+
14+
if len(stack) >= part_length and self._check_match(stack, part, part_length):
15+
for _ in range(part_length):
16+
stack.pop()
17+
18+
return ''.join(stack)
19+
20+
def _check_match(self, stack, part, part_length):
21+
return ''.join(stack[-part_length:]) == part
22+

0 commit comments

Comments
 (0)