626. Fibonacci Split
A researcher wrote down a long Fibonacci-style list of whole numbers but forgot the commas, leaving the digit string num. Cut num into pieces, read each piece as a non-negative whole number with no leading zeros (the piece 0 alone is allowed), and the list must satisfy three rules: it has at least three numbers, every number is at most 2147483647, and every number after the first two equals the sum of the previous two.
Return any list that satisfies the rules, written in order. If no such list exists, return an empty array. Any valid answer is accepted: a checker verifies the pieces, the sums and that the digits of the list, written one after another, give exactly num.
Example 1
- Input:
- num = "358132134"
- Output:
- [3,5,8,13,21,34]
- Explanation:
One valid list is
[3, 5, 8, 13, 21, 34]: every number after the first two is the sum of the previous two.
Example 2
- Input:
- num = "12346"
- Output:
- []
- Explanation:
No cut produces a valid list, so the answer is empty.
Example 3
- Input:
- num = "0000"
- Output:
- [0,0,0,0]
- Explanation:
Four zeros give
[0, 0, 0, 0], because 0 + 0 = 0 and the single digit 0 is allowed.
Constraints
- 1 ≤
num.length≤ 30 numconsists only of digits.
How this problem is judged
- Answers
- Any valid answer is accepted. A checker tests yours against the problem's rules.
Expected complexity
- Time
- O(n^2)
- Space
- O(n)