Tensorum
LeetCode 282 · Constraint Satisfaction

Expression Add Operators

LeetCode 282 · Hard · solution, intuition & step-by-step walkthrough

Track prev_operand to fix up multiplication precedence.

HardMathStringBacktracking
376 msruntime · beats 70%
19.5 MBmemory · beats 38%
1approach
6accepted runs · last 3 Apr 2026

This is how I solved Expression Add Operators (LeetCode 282), a hard problem from the Constraint Satisfaction pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

Try every way to cut the digit string into numbers and put +, − or * between them. Multiplication binds tighter, so remember the last operand: to apply *, undo it from the running value and add (last × current) instead.

Approach

  1. 1

    dfs(index, value, prev, expr).

  2. 2

    Try each next number num[index..i] (no leading zeros).

  3. 3

    First number starts the expression.

  4. 4

    +: value + cur; −: value − cur (prev = −cur); *: value − prev + prev·cur.

How I solved it

Backtracking with precedence fixPythonO(4ⁿ) time
class Solution:
    def addOperators(self, num: str, target: int) -> List[str]:
        res = []
        def dfs(index,cur_val,prev_operand,expression):
            if index==len(num):
                if cur_val==target:
                    res.append(expression)
                return
            for i in range(index,len(num)):
                if i!=index and num[index]=="0":
                    continue
                cur_str = num[index:i+1]
                cur_num = int(cur_str)
                if index==0:
                    dfs(i+1,cur_num,cur_num,cur_str)
                else:
                    #+
                    dfs(i+1,cur_val+cur_num,cur_num,expression+"+"+cur_str)
                    #-
                    dfs(i+1,cur_val-cur_num,-cur_num,expression+"-"+cur_str)
                    #*
                    #val-prevoperand+(prevoperand*cur_num)
                    dfs(i+1,cur_val-prev_operand+(prev_operand*cur_num),prev_operand * cur_num,expression+"*"+cur_str)
                    #(2,3*2,3-2+3*2, 1+2*3
        dfs(0,0,0,"")
        return res


            


                
Loading the step-through…

Expression Add Operators — FAQ

What is the intuition behind Expression Add Operators?

Try every way to cut the digit string into numbers and put +, − or * between them. Multiplication binds tighter, so remember the last operand: to apply *, undo it from the running value and add (last × current) instead.

How do you solve Expression Add Operators (LeetCode 282)?

1. dfs(index, value, prev, expr). 2. Try each next number num[index..i] (no leading zeros). 3. First number starts the expression. 4. +: value + cur; −: value − cur (prev = −cur); *: value − prev + prev·cur.

What is the time and space complexity of Expression Add Operators?

Time: O(4ⁿ). Space: O(n). (Accepted approach: Backtracking with precedence fix.)

What are common mistakes in Expression Add Operators?

Numbers like '05' are invalid — skip multi-digit numbers starting with 0.