aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--clear_digits.c28
-rw-r--r--clear_digits.py29
2 files changed, 57 insertions, 0 deletions
diff --git a/clear_digits.c b/clear_digits.c
new file mode 100644
index 0000000..3d751b6
--- /dev/null
+++ b/clear_digits.c
@@ -0,0 +1,28 @@
+// 3174. Clear Digits
+#include "leetcode.h"
+
+/*
+ * 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.
+ */
+
+char *clearDigits(char *s) {
+ int n = strlen(s), top = -1;
+ char *stack = (char *)malloc((n + 1) * sizeof(char));
+ for (int i = 0; i < n; i++) {
+ if (isdigit(s[i])) {
+ if (top >= 0)
+ top--;
+ } else
+ stack[++top] = s[i];
+ }
+ stack[++top] = '\0';
+ return stack;
+}
+
+int main() {
+ char *s1 = "abc", *s2 = "cb34";
+ printf("%s\n", clearDigits(s1)); // expect: abc
+ printf("%s\n", clearDigits(s2)); // expect: null
+}
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"))