A calculator widget shows a formula of non-negative whole numbers joined by +, -, and *, with no spaces and no brackets. A curious user wants to see what the formula could equal if brackets were added in different ways.
Write allGroupings(formula) that returns the value of every different way to fully add brackets, sorted ascending. Keep repeated values: if two different bracketings give the same value, list it twice.
Input: formula = "3-2-1" Output: [0,2]
(3-2)-1 = 0 and 3-(2-1) = 2.
Input: formula = "2*3+4" Output: [10,14]
(2*3)+4 = 10 and 2*(3+4) = 14.
1 <= formula.length <= 200 and 99.Run 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 #314 Every Way to Group a Formula. 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.