Split Array into Fibonacci Sequence
Medium · String, Backtracking
You are given a string of digits num, such as "123456579". We can split it into a Fibonacci-like sequence [123, 456, 579].
Formally, a Fibonacci-like sequence is a list f of non-negative integers such that:
Note that when splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number 0 itself.
Return any Fibonacci-like sequence split from num, or return [] if it cannot be done.
Example 1
Input: num = "1101111" Output: [11,0,11,11]
The output [110, 1, 111] would also be accepted.
Example 2
Input: num = "112358130" Output: []
The task is impossible.
Example 3
Input: num = "0123" Output: []
Leading zeroes are not allowed, so "01", "2", "3" is not valid.
Constraints
- 1 <= num.length <= 200
- num contains only digits.
Starter code (python)
class Solution(object):
def splitIntoFibonacci(self, num):
"""
:type num: str
:rtype: List[int]
"""
Related problems
- Longest Substring Without Repeating Characters (Medium)
- Longest Palindromic Substring (Medium)
- Roman to Integer (Easy)
Previous: Magic Squares In Grid · Next: Backspace String Compare · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.