Expression Add Operators
Imagine you are building a calculator app that can insert operators between digits to reach a target value. How do you find all valid expressions that evaluate exactly to that target?
Given a string num that contains only digits and an integer target, return all possibilities to add binary operators ('+', '-', or '*') between the digits so that the resultant expression evaluates to the target value. The returned expressions should not contain any spaces and must not have leading zeros in any operand except for the number zero itself.
1 ≤ num.length ≤ 10num consists of only digits.-2^31 ≤ target ≤ 2^31 - 1def addOperators(num: str, target: int) -> list:public List<String> addOperators(String num, int target)vector<string> addOperators(string num, int target)function addOperators(num, target)def addOperators(num: str, target: int) -> list:
# Write your solution here
passclass Solution {
public List<String> addOperators(String num, int target) {
// Write your solution here
return new ArrayList<>();
}
}#include <vector>
#include <string>
using namespace std;
vector<string> addOperators(string num, int target) {
// Write your solution here
return {};
}function addOperators(num, target) {
// Write your solution here
}1+2+3Missing multiplication operator expressions due to ignoring multiplication precedence.✅ Track last operand and adjust evaluated value when '*' operator is used: evaluated = evaluated - last_operand + last_operand * current.1*0+510-51*05Allowing operands with leading zeros like '05' which are invalid.✅ Add check to skip operands starting with '0' if length > 1.0+0+00+0-00-0+00-0-00*0*000+0Allowing multi-digit operands with leading zeros like '00'.✅ Only allow single '0' as operand; skip multi-digit operands starting with '0'.2+2+22+2+22+2+22*2*22*2*22*2*2Reusing digits or generating duplicate expressions due to incorrect index advancement.✅ Advance index correctly in recursion to use each digit exactly once and avoid duplicates.Solution times out on large inputs due to exponential complexity.✅ Implement pruning and efficient string building to reduce runtime.{"num":"123","target":6}["1+2+3","1*2*3"]Both expressions evaluate to 6: 1+2+3=6 and 1*2*3=6.
{"num":"232","target":8}["2*3+2","2+3*2"]Expressions '2*3+2' and '2+3*2' both evaluate to 8.
{"num":"","target":0}[]Empty input string yields no expressions.
{"num":"1","target":1}["1"]Single digit equal to target returns single expression without operators.
{"num":"000","target":0}["0+0+0","0+0-0","0+0*0","0-0+0","0-0-0","0-0*0","0*0+0","0*0-0","0*0*0"]All combinations of operators between zeros evaluate to 0; leading zeros allowed only for zero itself.
{"num":"105","target":5}["1*0+5","10-5"]Expressions with valid leading zero handling: '05' is invalid, so '1*0+5' and '10-5' are valid.
{"num":"1234","target":11}["1+2*3+4","1*2+3+4","1+2+3+4"]Expressions that require correct multiplication undo logic to evaluate properly. Note '1+2+3+4' also evaluates to 11 and is valid.
{"num":"222","target":6}["2+2+2","2+2*2","2*2*2"]Tests that operators are used exactly once per split and no unbounded reuse of operands. '2+2*2' also evaluates to 6 and is valid.
{"num":"1234567890","target":100}nullInput length n=10 with backtracking O(4^n) complexity must complete within 2 seconds.
