Skip to content

Commit b2ff1fa

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.6 MB (70.70%)
1 parent 3d6a4bb commit b2ff1fa

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

3447-clear-digits/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<p>You are given a string <code>s</code>.</p>
2+
3+
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
4+
5+
<ul>
6+
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
7+
</ul>
8+
9+
<p>Return the resulting string after removing all digits.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<div class="example-block">
15+
<p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p>
16+
17+
<p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p>
18+
19+
<p><strong>Explanation:</strong></p>
20+
21+
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
22+
</div>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">s = &quot;cb34&quot;</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>
30+
31+
<p><strong>Explanation:</strong></p>
32+
33+
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>&quot;c4&quot;</code>.</p>
34+
35+
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>&quot;&quot;</code>.</p>
36+
</div>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
43+
<li><code>s</code> consists only of lowercase English letters and digits.</li>
44+
<li>The input is generated such that it is possible to delete all digits.</li>
45+
</ul>

3447-clear-digits/solution.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Approach 2: Stack
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def clearDigits(self, s: str) -> str:
8+
stack = []
9+
10+
for char in s:
11+
if char.isdigit() and stack:
12+
stack.pop()
13+
else:
14+
stack.append(char)
15+
16+
return ''.join(stack)
17+

0 commit comments

Comments
 (0)