Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
deque (fewer assumptions) solution in Clear category for Letter Queue by adamspj
from collections import deque
def letter_queue(commands: list[str]) -> str:
queue = deque()
for command in commands:
if command == "POP" and queue: # Confirm the queue is not empty before popleft()
queue.popleft()
elif "PUSH" in command: # NOT assuming that PUSH and POP are the only valid commands
queue.append(command.split()[1])
return "".join(queue)
May 21, 2024
Comments: