← DiffPush

Infix to Postfix Conversion

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

The Compiler's Rearrangement Desk

A calculator firmware evaluates expressions on a stack machine that wants operators after their operands, yet engineers type expressions the human way with operators in between and parentheses overriding precedence. The firmware's translation desk rewrites each expression so that `a + b` becomes `a b +` while honoring that `^` outranks `*` and `/`, which outrank `+` and `-`.

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

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

Constraints

Examples

Example 1

Input: {"expression":"a+b*(c^d-e)^(f+g*h)-i"}
Output: "abcd^e-fgh*+^*+i-"
Parenthesized sub-expressions are resolved first, then multiplication/addition levels unwrap per precedence.

Example 2

Input: {"expression":"A*(B+C)/D"}
Output: "ABC+*D/"
B+C emits first; * then / follow, and equal precedence pops left-to-right.

Solve this in your browser →

Also on LeetCode ↗