Problem 563404 · medium · Phase 05 Advanced Algorithms & Graphs

Is It a Running-Sum Chain?

backtracking · strings · sequences

A number chain is a sequence of at least three non-negative integers where every number from the third on equals the sum of the two before it. A clerk wrote a chain as one digit string digits, without separators and without leading zeros (the number 0 itself is written "0"). Return True if digits can be split into such a chain, otherwise False.

Examples

Input:  digits = "358"
Output: True
Explanation: 3, 5, 8.

Input:  digits = "1023"
Output: False

Input:  digits = "000"
Output: True
Explanation: 0, 0, 0.

Constraints

  • 1 <= len(digits) <= 30, digits only

Goals

  • Recognise that the first two choices determine the whole sequence
  • Validate the rest of the string greedily after the choices
Starting Python…