-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path05_PostfixToInfix.java
37 lines (28 loc) · 1.34 KB
/
05_PostfixToInfix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Question Link: https://www.geeksforgeeks.org/problems/postfix-to-infix-conversion/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=postfix-to-prefix-conversion
class Solution {
// Time Complexity = O(n), Space Complexity = O(n)
static String postToInfix(String exp) {
// Stack to store the infix expressions
Stack<String> stack = new Stack<>();
// Traverse each character of the postfix expression
for (int i = 0; i < exp.length(); i++) {
char ch = exp.charAt(i);
// If the character is an operand, push it to the stack
if (Character.isLetterOrDigit(ch)) {
stack.push(ch + ""); // Convert char to string and push
}
// If the character is an operator
else {
// Pop two operands from the stack
String operand2 = stack.pop();
String operand1 = stack.pop();
// Create the new infix expression with parentheses
String newExp = "(" + operand1 + ch + operand2 + ")";
// Push the new expression back to the stack
stack.push(newExp);
}
}
// The final expression left in the stack is the infix expression
return stack.pop();
}
}