Convert an Infix Expression to Postfix Notation

Quick Overview

Convert an infix arithmetic expression with single-character operands, parentheses and the operators +, -, *, / and ^ into postfix notation. It tests stack-based expression parsing, operator precedence, and correct handling of left-associative and right-associative operators.

Convert an Infix Expression to Postfix Notation

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `expression` that holds a valid arithmetic expression in infix notation, convert it to postfix (Reverse Polish) notation and return the result as a string. The expression contains single-character operands (lowercase letters `a`-`z` or digits `0`-`9`), the operators `+`, `-`, `*`, `/` and `^`, and parentheses `(` and `)`. Operator precedence from lowest to highest is: `+` and `-`, then `*` and `/`, then `^`. The `^` operator is right-associative, while all other operators are left-associative. ### Function Signature ```python def infix_to_postfix(expression: str) -> str: ... ``` ### Rules - The input has no spaces. The output is the postfix sequence written as one string with no spaces or separators, one character per operand or operator. - Every operator is binary. There is no unary minus. - Parentheses never appear in the output. - Operators of equal precedence group by their associativity: `a-b-c` means `(a-b)-c`, and `a^b^c` means `a^(b^c)`. With these rules the postfix string is unique. ### Constraints - `1 <= expression.length <= 200` - `expression` contains only lowercase letters (`a`-`z`), digits (`0`-`9`), the operators `+`, `-`, `*`, `/`, `^`, and parentheses `(`, `)`. - `expression` is a valid infix expression. ### Examples ```text Input: expression = "a+b*(c^d-e)^(f+g*h)-i" Output: "abcd^e-fgh*+^*+i-" ``` The expression groups as `(a + (b * ((c^d - e) ^ (f + g*h)))) - i`. The two parenthesized groups become `cd^e-` and `fgh*+`, the `^` between them follows both, then come the `*` with `b`, the `+` with `a`, and last the subtraction of `i`. ```text Input: expression = "(a+b)*c" Output: "ab+c*" ``` The parentheses make `a+b` the left operand of `*`, so `ab+` is written first, then `c`, then `*`. ```text Input: expression = "a-b+c^d^e" Output: "ab-cde^^+" ``` The left-associative `-` and `+` group as `(a-b) + ...`, so `ab-` comes first. The right-associative `^` groups as `c^(d^e)`, so the inner `de^` is written before the outer `^`, and the `+` comes last.

Overview: Convert an infix arithmetic expression with single-character operands, parentheses and the operators +, -, *, / and ^ into postfix notation. It tests stack-based expression parsing, operator precedence, and correct handling of left-associative and right-associative operators.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string expression that holds a valid arithmetic expression in infix notation, convert it to postfix (Reverse Polish) notation and return the result as a string.

The expression contains single-character operands (lowercase letters a-z or digits 0-9), the operators +, -, *, / and ^, and parentheses ( and ).

Operator precedence from lowest to highest is: + and -, then * and /, then ^. The ^ operator is right-associative, while all other operators are left-associative.

Function Signature

def infix_to_postfix(expression: str) -> str:
    ...

Rules

  • The input has no spaces. The output is the postfix sequence written as one string with no spaces or separators, one character per operand or operator.
  • Every operator is binary. There is no unary minus.
  • Parentheses never appear in the output.
  • Operators of equal precedence group by their associativity: a-b-c means (a-b)-c , and a^b^c means a^(b^c) . With these rules the postfix string is unique.

Constraints

  • 1 <= expression.length <= 200
  • expression contains only lowercase letters ( a - z ), digits ( 0 - 9 ), the operators + , - , * , / , ^ , and parentheses ( , ) .
  • expression is a valid infix expression.

Examples

Input: expression = "a+b*(c^d-e)^(f+g*h)-i"
Output: "abcd^e-fgh*+^*+i-"

The expression groups as (a + (b * ((c^d - e) ^ (f + g*h)))) - i. The two parenthesized groups become cd^e- and fgh*+, the ^ between them follows both, then come the * with b, the + with a, and last the subtraction of i.

Input: expression = "(a+b)*c"
Output: "ab+c*"

The parentheses make a+b the left operand of *, so ab+ is written first, then c, then *.

Input: expression = "a-b+c^d^e"
Output: "ab-cde^^+"

The left-associative - and + group as (a-b) + ..., so ab- comes first. The right-associative ^ groups as c^(d^e), so the inner de^ is written before the outer ^, and the + comes last.