-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3174_Clear_Digits.swift
More file actions
56 lines (43 loc) · 1.33 KB
/
3174_Clear_Digits.swift
File metadata and controls
56 lines (43 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Done 02.06.2025. Revisited: N/A
You are given a string s.
Your task is to remove all digits by doing this operation repeatedly:
Delete the first digit and the closest non-digit character to its left.
Return the resulting string after removing all digits.
Note that the operation cannot be performed on a digit that does not have any non-digit character to its left.
Example 1:
Input: s = "abc"
Output: "abc"
Explanation:
There is no digit in the string.
Example 2:
Input: s = "cb34"
Output: ""
Explanation:
First, we apply the operation on s[2], and s becomes "c4".
Then we apply the operation on s[1], and s becomes "".
Constraints:
1 <= s.length <= 100
s consists only of lowercase English letters and digits.
The input is generated such that it is possible to delete all digits.
*/
import Foundation
class P3174 {
// MARK: - Option 1. Time: O(?). Memory: O(?)
func clearDigits(_ s: String) -> String {
let stack = CharStack()
for ch in s {
if ch.isNumber {
if (stack.top()?.isNumber == false) {
_ = stack.pop()
} else {
stack.push(ch)
}
} else {
stack.push(ch)
}
}
return stack.joined()
}
// MARK: - Option 2. Time: O(?). Memory: O(?)
}