Skip to content

Commit 2707525

Browse files
committed
Sync LeetCode submission Runtime - 583 ms (24.63%), Memory - 18 MB (69.64%)
1 parent a8db596 commit 2707525

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>&#39;+&#39;</code><em>, </em><code>&#39;-&#39;</code><em>, and/or </em><code>&#39;*&#39;</code><em> between the digits of </em><code>num</code><em> so that the resultant expression evaluates to the </em><code>target</code><em> value</em>.</p>
2+
3+
<p>Note that operands in the returned expressions <strong>should not</strong> contain leading zeros.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> num = &quot;123&quot;, target = 6
10+
<strong>Output:</strong> [&quot;1*2*3&quot;,&quot;1+2+3&quot;]
11+
<strong>Explanation:</strong> Both &quot;1*2*3&quot; and &quot;1+2+3&quot; evaluate to 6.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> num = &quot;232&quot;, target = 8
18+
<strong>Output:</strong> [&quot;2*3+2&quot;,&quot;2+3*2&quot;]
19+
<strong>Explanation:</strong> Both &quot;2*3+2&quot; and &quot;2+3*2&quot; evaluate to 8.
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> num = &quot;3456237490&quot;, target = 9191
26+
<strong>Output:</strong> []
27+
<strong>Explanation:</strong> There are no expressions that can be created from &quot;3456237490&quot; to evaluate to 9191.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= num.length &lt;= 10</code></li>
35+
<li><code>num</code> consists of only digits.</li>
36+
<li><code>-2<sup>31</sup> &lt;= target &lt;= 2<sup>31</sup> - 1</code></li>
37+
</ul>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Approach 1: Backtracking
2+
3+
# Time: O(n * 4^n)
4+
5+
class Solution:
6+
def addOperators(self, num: str, target: int) -> List[str]:
7+
n = len(num)
8+
answers = []
9+
10+
def recurse(index, prev_operand, curr_operand, value, string):
11+
if index == n:
12+
# If the final value == target expected AND
13+
# no operand is left unprocessed
14+
if value == target and curr_operand == 0:
15+
answers.append(''.join(string[1:]))
16+
return
17+
18+
# Extending the current operand by one digit
19+
curr_operand = curr_operand * 10 + int(num[index])
20+
str_op = str(curr_operand)
21+
22+
# To avoid cases where we have 1 + 05 or 1 * 05
23+
if curr_operand > 0:
24+
# NO OP Recursion
25+
recurse(index + 1, prev_operand, curr_operand, value, string)
26+
27+
# Addition
28+
string.append('+')
29+
string.append(str_op)
30+
recurse(index + 1, curr_operand, 0, value + curr_operand, string)
31+
string.pop()
32+
string.pop()
33+
34+
# Can substract or multiply only if there are some previous operands
35+
if string:
36+
# Subtraction
37+
string.append('-')
38+
string.append(str_op)
39+
recurse(index + 1, -curr_operand, 0, value - curr_operand, string)
40+
string.pop()
41+
string.pop()
42+
43+
# Multiplication
44+
string.append('*')
45+
string.append(str_op)
46+
recurse(index + 1, curr_operand * prev_operand, 0, value - prev_operand + (curr_operand * prev_operand), string)
47+
string.pop()
48+
string.pop()
49+
50+
recurse(0, 0, 0, 0, [])
51+
return answers
52+

0 commit comments

Comments
 (0)