#K68562. Expression Add Operators
Expression Add Operators
Expression Add Operators
You are given a non‐empty string num that contains only digits ('0'-'9') and an integer target. Your task is to insert binary operators (+
, -
, *
) between the digits so that the resulting mathematical expression evaluates to the target value.
Note that operands in the returned expressions should not contain extra leading zeros. You must return all valid expressions that evaluate to the target value.
Mathematically, for an expression expr formed by numbers and operators, its evaluation must satisfy:
$$ eval(expr) = target $$
If no valid expression exists, output a single line containing []
.
Input Format: The first line contains the string num
, and the second line contains the integer target
.
Output Format: If there is at least one valid expression, output each expression on a new line in lexicographical order. If there are no valid expressions, output a single line with []
.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line is a string
num
containing only digits. - The second line is an integer
target
.
outputFormat
Print all valid expressions (one per line) that evaluate to target
in lexicographical order. If no valid expressions exist, print a single line with []
.
123
6
1*2*3
1+2+3
</p>