Prefix to Postfix Conversion

Prefix, Infix, Postfix Conversion Problems DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Prefix to Postfix Conversion, Strings, String Manipulation, Stack, Algorithm, Time Complexity, Space Complexity, expression conversion, expression notation, stack, Infix, Prefix, Postfix Notations.

Problem Statement Given a prefix expression, convert it to its equivalent postfix expression. A prefix expression (also known as Polish notation) is an arithmetic expression in which the operator precedes its operands. For example, +AB is a prefix expression for A+B. A postfix expression (also known as Reverse Polish notation) is an arithmetic expression in which the operator follows its operands. For example, AB+ is a postfix expression for A+B. You will be given a valid prefix expression consisting of uppercase English letters as operands and the operators +, , , /. Input Specification The input will consist of a single line containing a string S, representing the prefix expression. Output Specification Print a single line containing the equivalent postfix expression. Constraints 1 <= |S| <= 10^5 (where |S| is the length of the string S) S will only contain uppercase English letters (A Z) and the operators +, , , /. It is guaranteed that the given prefix expression is valid. Sample Test Cases Sample Input 1 Sample Output 1 Explanation Given the prefix expression +AB/CD: 1. Scan 'D': Push 'D' to stack. 2. Scan 'C': Push 'C' to stack. 3. Scan '/': Pop 'C', Pop 'D'. Form "CD/". Push "CD/" to stack. 4. Scan 'B': Push 'B' to stack. 5. Scan 'A': Push 'A' to stack. 6. Scan '+': Pop 'A', Pop 'B'. Form "AB+". Push "AB+" to stack. 7. Scan ' ': Pop "AB+", Pop "CD/". Form "AB+CD/ ". Push "AB+CD/ " to stack. The final element in the stack is the postfix expression: AB+CD/ . Difficulty Medium