Show problems as
Classic problem #306MediumStringBacktracking

Additive Number

Real-world version: Consistent Stock-Taking Tape
Tracked in this browser only
Read Additive Number on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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