Skip to content

Commit c458941

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.8 MB (27.74%)
1 parent 252918c commit c458941

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def clearDigits(self, s: str) -> str:
3+
res = ""
4+
for c in s:
5+
if c.isdigit() and res:
6+
res = res[:-1] # remove the last character
7+
else:
8+
res += c # add character to result
9+
return res

0 commit comments

Comments
 (0)