Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Note that a number can contain multiple digits.
Since the question asks us to find <b>all</b> of the valid expressions, we need a way to iterate over all of them. (<b>Hint:</b> Recursion!)
We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expression's value as well so as to avoid the evaluation at the very end of recursion?
Think carefully about the multiply operator. It has a higher precedence than the addition and subtraction operators. <br> 1 + 2 = 3 <br> 1 + 2 - 4 --> 3 - 4 --> -1 <br> 1 + 2 - 4 * 12 --> -1 * 12 --> -12 (WRONG!) <br> 1 + 2 - 4 * 12 --> -1 - (-4) + (-4 * 12) --> 3 + (-48) --> -45 (CORRECT!)
We simply need to keep track of the last operand in our expression and reverse it's effect on the expression's value while considering the multiply operator.