blob: 3d751b6fd92c51d514d3b17d1af2c89fe0fba446 (
plain)
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
|
// 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
}
|