← DiffPush

Prefix to Postfix Conversion

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

The Sensor Feed Translator

A fleet of sensor controllers emits commands operator-first, but the diagnostics dashboard only accepts operator-last feeds. The translator walks the command string backwards, keeps partially assembled feed fragments on a rack, and on each operator concatenates the two rack-top fragments with the operator moved to the end.

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

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

Constraints

Examples

Example 1

Input: {"expression":"*-A/BC-/AKL"}
Output: "ABC/-AK/L-*"
Each backward step relocates its operator to the tail of the two fragments it joins.

Example 2

Input: {"expression":"+AB"}
Output: "AB+"
The two-operand minimal case: operands first, operator last.

Solve this in your browser →

Also on LeetCode ↗