
You are given a string digits containing only the digits '0' through '9', and an integer target. Your task is to find all possible ways to insert the binary operators +, -, or * between the digits to form expressions that evaluate to the target value.
Rules for forming expressions:
1) Operators are placed between digits.
2) Operands (the numbers in the expression) can be formed by concatenating multiple digits. For example, in "123", you can form the operand 12.
3) Operands cannot have leading zeros, unless the number is 0 itself. For example, "105" can form 10, but "05" is invalid.
You need to return a list of all unique strings that represent these valid expressions.
The first line contains the string digits.
The second line contains the integer target.
Your function should return a list of strings, where each string is a valid expression. The runner code will handle printing each expression on a new line. If no solutions are found, nothing will be printed.
This problem requires a backtracking approach. A recursive helper function is needed to build the expression step-by-step.
124
9
1+2*4
125
7
1*2+5
12-5
The expected time complexity is O(N * 4^N).
1 <= digits.length <= 10
-2^31 <= target <= 2^31 - 1
Time limit: 1 sec