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
andmakes failures fall through to later branches. ch.lower() == chdoes not detect letters; symbols pass too. Usech.isalpha()or justelse.
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.
