Ctrl K

Chain of Sums in a Digit String

Medium
You are given a string digits made only of digits. Decide whether it can be cut into a sequence of numbers (keeping the digits in order and using each digit exactly once) such that: - there are at least three numbers; - every number from the third one on equals the sum of the two numbers right before it. A number may not start with a zero unless it is 0 itself: pieces like 05 or 00 are not allowed, while 0 is fine. Return True if such a cut exists and False otherwise. The numbers in the chain can get very long; think about how you would cope in a language without big integers.
Example 1
digits='2810182846' → True

Cut it as 2, 8, 10, 18, 28, 46. From the third number on, each one is the sum of the two before it: 2 + 8 = 10, 8 + 10 = 18, 10 + 18 = 28, 18 + 28 = 46.

Example 2
digits='1012' → False

The cut 1, 01, 2 would work, but "01" has a leading zero, which is not allowed. No other cut follows the sum rule.

Example 3
digits='5050100' → True

50 + 50 = 100: a chain of three numbers is enough.

Constraints
  • 1 ≤ len(digits) ≤ 100
  • digits contains only the digits 0–9
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsKeypad Words MediumAll Balanced Bracket Strings MediumWord Traced on a Letter Grid MediumIP Addresses From a Digit String MediumCutting a Word Into Palindromes Medium
loading Python…
Input
digits='2810182846'
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 82 tests on the server, hidden ones included.