Minimize Result by Adding Parentheses to Expression LeetCode solution in Python
1class Solution:2def minimizeResult(self, expression: str) -> str:3plus_index, n, ans = (4expression.find("+"),5len(expression),6[float(inf), expression],7)89def evaluate(exps: str):10return eval(11exps.replace("(", "*(").replace(")", ")*").lstrip("*").rstrip("*")12)1314for l in range(plus_index):15for r in range(plus_index + 1, n):16exps = f"{expression[:l]}({expression[l:plus_index]}+{expression[plus_index+1:r+1]}){expression[r+1:n]}"17res = evaluate(exps)18if ans[0] > res:19ans[0], ans[1] = res, exps20return ans[1]