File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Expand file tree Collapse file tree 2 files changed +54
-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+ 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
You can’t perform that action at this time.
0 commit comments