aboutsummaryrefslogtreecommitdiff
path: root/remove_star_string.c
diff options
context:
space:
mode:
Diffstat (limited to 'remove_star_string.c')
-rw-r--r--remove_star_string.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/remove_star_string.c b/remove_star_string.c
new file mode 100644
index 0000000..10a2862
--- /dev/null
+++ b/remove_star_string.c
@@ -0,0 +1,40 @@
+// 2390. Removing Stars From a String
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * given a string 's', which contains stars (*). in one operation
+ * you can choose a start in 's', remove the closest non-star
+ * character to its left, as well remove the star itself.
+ * return string after all stars have been removed. note, the
+ * input will be generated such that the operation is always
+ * possible, and the resulting string will always be unique
+ */
+
+char *removeStars(char *s) {
+ int n = strlen(s), idx = 0;
+ char *stk = malloc((n + 1) * sizeof(char));
+ for (int i = 0; i < n; i++) {
+ stk[idx] = s[i];
+ if (stk[idx] == '*') {
+ if (!idx)
+ continue;
+ else
+ idx -= 2;
+ }
+ idx++;
+ }
+ stk[idx] = '\0';
+ return stk;
+}
+
+int main() {
+ char s1[] = {"leet**cod*e"}, s2[] = {"erase*****"};
+ char *ans1 = removeStars(s1), *ans2 = removeStars(s2);
+ for (int i = 0; i < strlen(s1); i++)
+ printf("%c", ans1[i]); // expect: lecoe
+ printf("\n");
+ for (int i = 0; i < strlen(s2); i++)
+ printf("%c", ans2[i]); // expect: null
+}