-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefix_to_infix_conversion.py
55 lines (38 loc) · 1.24 KB
/
prefix_to_infix_conversion.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
You are given a string S of size N that represents the prefix form of a valid mathematical expression. Convert it to its infix form.
Example 1:
Input:
*-A/BC-/AKL
Output:
((A-(B/C))*((A/K)-L))
Explanation:
The above output is its valid infix form.
Your Task:
Complete the function string preToInfix(string pre_exp), which takes a prefix string as input and return its infix form.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
3<=|S|<=104
"""
class Solution:
def isOperator(self, c):
if c == "*" or c == "+" or c == "-" or c == "/" or c == "^" or c == "(" or c == ")":
return True
else:
return False
def preToInfix(self, pre_exp):
# Code here
stack = []
# read prefix in reverse order
i = len(prefix) - 1
while i >= 0:
if not self.isOperator(prefix[i]):
# symbol is operand
stack.append(prefix[i])
i -= 1
else:
# symbol is operator
str = "(" + stack.pop() + prefix[i] + stack.pop() + ")"
stack.append(str)
i -= 1
return stack.pop()