← DiffPush

Infix to Prefix Conversion

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

The Reverse-Reading Billing Parser

A billing engine feeds expressions to a mainframe whose dialect writes operators *before* their operands (`+ a b` instead of `a + b`). The conversion desk has a trick: read the expression right-to-left, swap the parenthesis roles, and let the same precedence machinery run — then flip the assembled result to restore reading order. Implement that desk.

Input: A string expression in infix form using single alphanumeric operands, the operators + - * / ^, and parentheses.

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

Constraints

Examples

Example 1

Input: {"expression":"((A-(B/C))*((A/K)-L))"}
Output: "*-A/BC-/AKL"
The fully parenthesized input converts unambiguously; reading backwards then reversing produces the operator-first form.

Example 2

Input: {"expression":"A+B*C"}
Output: "+A*BC"
Multiplication binds tighter, so the addition ends up as the outermost operator.

Solve this in your browser →

Also on LeetCode ↗