A spreadsheet puzzle shows a row of digits and a target value. The player may put +, -, or * between any two neighboring digits, or nothing, which joins the digits into a longer number. Normal math rules apply: * is done before + and -.
A number with more than one digit can't start with 0 (05 is not allowed, but 0 alone is fine).
Write formulasForTarget(digits, target) that returns every formula whose value equals target, sorted by plain string comparison (so * comes before +, which comes before -, which comes before digits). Return [] if there is none.
Input: digits = "123", target = 6 Output: ["1*2*3","1+2+3"]
1*2*3 = 6 and 1+2+3 = 6. * sorts before +.
Input: digits = "232", target = 8 Output: ["2*3+2","2+3*2"]
Multiplication happens first: 2*3+2 = 8 and 2+3*2 = 8.
1 <= digits.length <= 8digits only contains 0-9.-10^8 <= target <= 10^8Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #290 Spreadsheet Formula Search. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.