File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change 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 >  ; </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 = " ; abc" ; </span ></p >
16+
17+ <p ><strong >Output:</strong > <span class =" example-io " >" ; abc" ; </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 = " ; cb34" ; </span ></p >
28+
29+ <p ><strong >Output:</strong > <span class =" example-io " >" ;" ; </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 >" ; c4" ; </code >.</p >
34+
35+ <p >Then we apply the operation on <code >s[1]</code >, and <code >s</code > becomes <code >" ;" ; </code >.</p >
36+ </div >
37+
38+ <p >  ; </p >
39+ <p ><strong >Constraints:</strong ></p >
40+
41+ <ul >
42+ <li><code>1 <= s.length <= 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 >
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments