↓Skip to main content
  1. LeetCode/

LeetCode 3612: Process String with Special Operations I

·2 mins· ·
LeetCode Medium String Simulation
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-13

  • Submissions: Wrong Answer → Accepted

Approach
#

Simulate the rules (String Template 1). With a list as the result, * pops, # is res += res, % reverses, a letter appends, and join at the end.

My Attempts
#

Attempt 1 wrote if char == "*" and res:. With an empty result that condition fails, so * falls through to the last elif char.lower() == char, and since "*".lower() is still "*", it was appended as if it were a letter. For "*" the answer is the empty string; it returned "*". Moving if res inside the branch passed.

Solution
#

class Solution:
    def processStr(self, s: str) -> str:
        res = []
        for ch in s:
            if ch == "*":
                if res:
                    res.pop()
            elif ch == "#":
                res += res
            elif ch == "%":
                res.reverse()
            else:
                res.append(ch)
        return "".join(res)

Complexity
#

The result can double at every #; time and space are proportional to the final length.

Mistakes to Avoid
#

  • Folding an extra condition into a branch test with and makes failures fall through to later branches.
  • ch.lower() == ch does not detect letters; symbols pass too. Use ch.isalpha() or just else.

Interview Explanation
#

I simulate the four operations on a list and join at the end. Note that * on an empty result does nothing; that check must sit inside the branch, or * falls through and is treated as a letter. Time is proportional to the output length.

Related

LeetCode 43: Multiply Strings
·2 mins
LeetCode Medium Math String Simulation
LeetCode 43 note: num1[i] × num2[j] lands at position i + j + 1; accumulate into an array of length n + m and carry at the end
LeetCode 3121: Count the Number of Special Characters II
·2 mins
LeetCode Medium Hash-Table String
LeetCode 3121 note: track each letter’s last lowercase index and first uppercase index; it counts only when the former comes first
LeetCode 735: Asteroid Collision
·2 mins
LeetCode Medium Array Stack Simulation
LeetCode 735 note: scan left to right with a stack; only a right-moving top meeting a left-moving asteroid collides, the smaller explodes and equal sizes both do
LeetCode 402: Remove K Digits
·2 mins
LeetCode Medium String Stack Greedy Monotonic-Stack
LeetCode 402 note: a monotonically increasing stack; while deletions remain, pop any top larger than the current digit, then strip leading zeros
LeetCode 187: Repeated DNA Sequences
·1 min
LeetCode Medium Hash-Table String Bit-Manipulation Sliding-Window
LeetCode 187 note: count every length-10 substring in a hash map and return those seen more than once
LeetCode 227: Basic Calculator II
·2 mins
LeetCode Medium Math String Stack
LeetCode 227 note: read whole numbers, and at each operator apply the previous one: + and - push the number (negated for -), * and / combine with the top