Trains the technique from
LeetCode 241Different Ways to Add ParenthesesThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
An arithmetic line is given as expression, made of whole numbers and the signs '+', '-' and '*', with no spaces and no signs marking a number as negative.
Brackets may be put anywhere, so long as the result is a valid arithmetic line, which fixes the order the operations happen in. Different bracketings may give different values.
Return the values from every possible bracketing, in increasing order. A value appears as many times as there are bracketings producing it.
Example 1
Bracketing the first subtraction gives minus five, and bracketing the second gives three.
Example 2
There is no sign to bracket, so the line takes its own value.
Example 3
One sign means one bracketing.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def diff_ways_to_compute(expression: str) -> list[int]:public List<Integer> diffWaysToCompute(String expression)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.