The problem. Given a valid postfix expression (operator after its operands), convert it to prefix (operator before its operands). Operands are single letters or digits; operators are + - * / ^. For example, AB+CD-* becomes *+AB-CD.
Input: "AB+CD-*" Output: "*+AB-CD"
Input: "ABC/-AK/L-*" Output: "*-A/BC-/AKL"
Input: "ABC*+DE/-" Output: "-+A*BC/DE"This is the mirror image of Prefix to Postfix. Postfix is read naturally from the left: when an operator appears, its two operands are the two most recent finished pieces — a stack's top two items.
Find two finished operands followed directly by an operator, like AB+, and replace those three items with one prefix piece, +AB. Repeat until one piece is left.
function postfixToPrefix(s) { const items = s.split("").map((c) => ({ text: c, op: "+-*/^".includes(c) })); while (items.length > 1) { for (let i = 0; i + 2 < items.length; i++) { // two finished operands followed by an operator are an innermost operation if (!items[i].op && !items[i + 1].op && items[i + 2].op) { const piece = items[i + 2].text + items[i].text + items[i + 1].text; items.splice(i, 3, { text: piece, op: false }); // the piece is an operand now break; // the list changed: start over } } } return items[0].text;}Time O(n²), space O(n). One rescan per operator.
Postfix describes itself from the right: the last character is the root operator (or a lone operand). Reading backwards, an operator is preceded by its right operand and, before that, its left operand — so build the right one first, then the left one, and return operator + left + right.
function postfixToPrefix(s) { let i = s.length - 1; // read from the end function build() { const c = s[i--]; if (!"+-*/^".includes(c)) return c; // an operand is complete on its own const right = build(); // reading backwards, the right operand comes first const left = build(); return c + left + right; // the operator goes first } return build();}Time O(n), space O(n) for the recursion, with the usual risk of overflowing the call stack on very long, lopsided expressions. (In Java, c + left + right works because left is a String: the char is joined as text, not added as a number.)
Read forwards. Push every operand. When an operator arrives, its two operands are the top two items — and because we read forwards this time, the right operand is on top. Pop right, pop left, and push operator + left + right.
Reading forwards puts the right operand on top — the opposite of the prefix scans.
Time O(n), space O(n), iterative. One rule covers all of these conversions: start reading from the end where operands come first — the right end for prefix, the left end for postfix. Reading forwards, the first pop is the right operand; reading backwards, it's the left one.
O(n²)One rescan of the expression per operator.
O(n)The string being rewritten.
O(n)"A" stays "A"."AB-" must become "-AB", not "-BA"."AB+C+D+" → "+++ABCD"."ABC^^" → "^A^BC".Every character is read once.
O(n)One call per operator on the stack.
O(n)Each character is pushed once; each operator pops two.
O(n)The explicit stack, which can't overflow the call stack.