commit c151ea288ff0e52214030607a7a4c30c71193cd2
parent 36854e82f1b5605249853a1213464cab62ee70a6
Author: Jack Sangdahl <jack@pngu.org>
Date: Tue, 16 Jun 2026 11:44:31 -0600
3612. process string with special operations 1
Diffstat:
2 files changed, 88 insertions(+), 0 deletions(-)
diff --git a/process_str_special_op1.c b/process_str_special_op1.c
@@ -0,0 +1,56 @@
+// 3612. Process String with Special Operations I
+#include "leetcode.h"
+
+/*
+ * you are given a string 's' containing lowercase english letters and the
+ * special characters '*', '#', and '%'. build a new string 'result' by
+ * processing 's' according to the following rules from left to right: if the
+ * letter is a lowercase english letter append it to 'result'. a '*' removes the
+ * last character from 'result'. a '#' duplicates the current 'result' and
+ * appends it to itself. a '%' reverses the current 'result'. return the
+ * resulting string.
+ */
+
+char *processStr(char *s) {
+ int cap = 1024, n = 0;
+ char *ans = (char *)malloc(cap * sizeof(char));
+ for (int i = 0; s[i] != '\0'; i++) {
+ char c = s[i];
+ if (c == '*') {
+ if (n > 0)
+ n--;
+ } else if (c == '#') {
+ while (n * 2 + 1 > cap) {
+ cap *= 2;
+ ans = (char *)realloc(ans, cap);
+ }
+ memcpy(ans + n, ans, n);
+ n *= 2;
+ } else if (c == '%') {
+ for (int l = 0, r = n - 1; l < r; l++, r--) {
+ char temp = ans[l];
+ ans[l] = ans[r];
+ ans[r] = temp;
+ }
+ } else {
+ if (n + 2 > cap) {
+ cap *= 2;
+ ans = (char *)realloc(ans, cap);
+ }
+ ans[n++] = c;
+ }
+ }
+ ans[n] = '\0';
+ return ans;
+}
+
+int main() {
+ char *s1 = "a#b%*", *r1 = "ba";
+ char *s2 = "z*#", *r2 = "";
+ char *ps1 = processStr(s1);
+ char *ps2 = processStr(s2);
+ printf("%s\n", ps1);
+ assert(!strcmp(ps1, r1));
+ printf("%s\n", ps2);
+ assert(!strcmp(ps2, r2));
+}
diff --git a/process_str_special_op1.py b/process_str_special_op1.py
@@ -0,0 +1,32 @@
+# 3612. Process String with Special Operations I
+
+"""
+you are given a string 's' containing lowercase english letters and the
+special characters '*', '#', and '%'. build a new string 'result' by
+processing 's' according to the following rules from left to right: if the
+letter is a lowercase english letter append it to 'result'. a '*' removes the
+last character from 'result'. a '#' duplicates the current 'result' and
+appends it to itself. a '%' reverses the current 'result'. return the
+resulting string.
+"""
+
+
+class Solution:
+ def processStr(self, s: str) -> str:
+ ans = ""
+ for c in s:
+ if c == "*":
+ ans = ans[:-1]
+ elif c == "#":
+ ans += ans
+ elif c == "%":
+ ans = ans[::-1]
+ else:
+ ans += c
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.processStr(s="a#b%*"))
+ print(obj.processStr(s="z*#"))