← DiffPush

Postfix to Prefix Conversion

Standard Bar Stack and Queues · Infix, Postfix, and Prefix O(n) · O(n)

The Diagnostics Channel Swapper

Legacy diagnostics devices broadcast commands operator-last, while the modern control bus expects operator-first messages. The gateway scans each incoming command left-to-right, holding partially translated messages on a buffer; on every operator it re-emits the operator first, followed by the two buffered messages in swapped order.

Input: A string expression in valid postfix form with single alphanumeric operands.

Output: Return the equivalent prefix (Polish) expression as a string.

Constraints

Examples

Example 1

Input: {"expression":"ABC/-AK/L-*"}
Output: "*-A/BC-/AKL"
Each operator is hoisted to the front of the two fragments it merges, inverting the postfix layout.

Example 2

Input: {"expression":"ab+c*"}
Output: "*+abc"
The + merges a and b first; the * then fronts the whole group with c.

Solve this in your browser →

Also on LeetCode ↗