Predict the output
What does this print?
typed = "ab##c#d"
stack: list[str] = []
for ch in typed:
if ch == "#":
if stack:
stack.pop()
else:
stack.append(ch)
print("".join(stack))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A d
Right: a and b are erased, c is erased, d remains.
Every backspace undoes the most recent character, which is exactly what popping a stack does.
Why not the others?
- B cd. The third '#' erases the c.
- C abcd. Each '#' removes the latest kept character.