987. Postfix to Infix Conversion

You are given a valid postfix expression as a string, where:

  • Operands are single lowercase English letters ('a' to 'z')
  • Operators are binary: '+', '-', '*', '/'

The expression contains no spaces and is guaranteed to be syntactically valid.

Write a function to convert the given postfix expression into a valid infix expression.

Use parentheses to clearly represent the evaluation order of the expression.

Example 1:

Input: "ab+"

Output: "(a+b)"

Explanation:

  •  postfix : a b +
  •  infix  : (a + b)

Example 2:

Input: "abc*+"

Output: "(a+(b*c))"

Now Your Turn!

Pick the correct output for the given input

Input: "abc/-ak/l-*"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ postExp.length ≤ 16000
  • postExp consists only of lowercase letters (a‒z) as operands and the four binary operators + - * /.
  • The given postfix expression is guaranteed valid (every operator has exactly two operands).
0
class Solution {
public:
string postToInfix(string postExp) {
// Your code goes here
}
};
 
Test Case

Input:

Expression