aboutsummaryrefslogtreecommitdiff
path: root/clear_digits.py
diff options
context:
space:
mode:
Diffstat (limited to 'clear_digits.py')
-rw-r--r--clear_digits.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/clear_digits.py b/clear_digits.py
new file mode 100644
index 0000000..d274575
--- /dev/null
+++ b/clear_digits.py
@@ -0,0 +1,29 @@
+# 3174. Clear Digits
+
+"""
+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.
+"""
+
+
+class Solution(object):
+ def clearDigits(self, s):
+ """
+ :type s: str
+ :rtype: str
+ """
+ stack = []
+ for c in s:
+ if c.isdigit():
+ if stack:
+ stack.pop()
+ else:
+ stack.append(c)
+ return "".join(stack)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.clearDigits(s="abc"))
+ print(obj.clearDigits(s="cb34"))