-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.py
More file actions
43 lines (39 loc) · 1.5 KB
/
Copy pathsolution.py
File metadata and controls
43 lines (39 loc) · 1.5 KB
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
class Solution:
def findExpr(self, s, target):
"""
s: string of digits
target: integer target
returns: list of expression strings that evaluate to target
"""
res = []
n = len(s)
if n == 0:
return res
def dfs(pos, expr, curVal, last):
# pos: current index in s we are processing
# expr: current expression string
# curVal: evaluated value of expr
# last: last operand (signed) that was added to curVal
if pos == n:
if curVal == target:
res.append(expr)
return
for i in range(pos, n):
# skip numbers with leading zero
if i > pos and s[pos] == '0':
break
numStr = s[pos:i+1]
val = int(numStr)
if pos == 0:
# first number, we start the expression with it
dfs(i + 1, numStr, val, val)
else:
# plus
dfs(i + 1, expr + '+' + numStr, curVal + val, val)
# minus
dfs(i + 1, expr + '-' + numStr, curVal - val, -val)
# multiply: adjust previous contribution
dfs(i + 1, expr + '*' + numStr, curVal - last + last * val, last * val)
dfs(0, "", 0, 0)
res.sort() # lexicographic order
return res