3

Infix To Postfix

Difficulty: EASY
Contributed By
Aditya Agarwal
Avg. time to solve
20 min
Success Rate
80%

Problem Statement
Suggest Edit

You are given a string EXP which is a valid infix expression. Convert the given infix expression to postfix expression.

Infix expression is of the form a op b. Where operator is is between the operands.

Postfix expression is of the form a b op. where the operator is after the operands.

Expression contains digits, lower case English letters, ‘(’, ‘)’, ‘+’, ‘-’, ‘*’, ‘/’, ‘^’.

For example:

EXP = ‘3+4*8’
Here multiplication is performed first and then the addition operation 
ANS = 3 4 8 * +
Input Format:
The first line of input contains an integer T’ denoting the number of test cases to run. Then the test case follows.

The first line of each test case contains a string ‘EXP’, a valid infix expression. 
Output Format:
For each query print, the valid postfix expression of the given infix expression.

Output for each query is printed in a separate line.
Note:
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 5000 

‘N’, is the length of EXP
The expression contains digits, lower case English letters, ‘(’, ‘)’, ‘+’, ‘-’, ‘*’, ‘/’, ‘^’. 

Time Limit: 1 sec
Sample Input 1:
2
3^(1+1)
3^1+1
Sample Output 1:
311+^
31^1+
Explanation of Sample Input 1:
For the first test case, addition is performed first as it is in parenthesis and then the power
In evaluating “3 1 1 + ^” it becomes “3 2 ^” and then “9” 

For the second test case power is performed first and then the addition.
In evaluation “31^1+” it becomes “31+” then becomes “4”
Sample Input 2:
2
a
a+b+c+d-e
Sample Output 2:
a
ab+c+d+e-
Reset Code
Full screen
copy-code
Console