All problems
0705MediumStringBacktracking

Consistent Stock-Taking Tape

Tracked in this browser only
Write code

Trains the technique from

LeetCode 306Additive Number

This 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.

A stock-taking machine prints its readings onto a paper tape as one unbroken run of digits with nothing separating them. tape is that run.

The tape is consistent when its digits can be cut, left to right and with no digit left over, into three or more whole numbers such that every number from the third onwards equals the sum of the two numbers immediately before it.

No number in the cut may carry a leading zero. A single 0 is a legal number, but 07 and 012 are not.

Return true when the tape is consistent, and false otherwise.

Examples

Example 1

Input
tape = "48122032"
Output
true

Cutting the tape as 4, 8, 12, 20, 32 uses every digit, and `4 + 8 = 12`, `8 + 12 = 20` and `12 + 20 = 32`, so the tape is consistent.

Example 2

Input
tape = "9099"
Output
true

Cutting the tape as 9, 0, 9, 9 uses every digit, and `9 + 0 = 9` and `0 + 9 = 9`. The single `0` is a legal number, so the tape is consistent.

Example 3

Input
tape = "5713"
Output
false

The cuts into three or more numbers are 5, 7, 1, 3 / 5, 7, 13 / 5, 71, 3 / 57, 1, 3, and none of them has every number from the third onwards equal to the sum of the two before it, so the tape is not consistent.

Constraints

  • 1 <= tape.length <= 35
  • tape consists only of digits.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def is_summing_tape(tape: str) -> bool:
Java
public boolean isSummingTape(String tape)
September 7
Apply