Split Array into Fibonacci Sequence

LeetCode Q 842 - Split Array into Fibonacci Sequence

Given a string S of digits, such as S = "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:

  • 0 <= F[i] <= 2^31 - 1, (that is, each integer fits a 32-bit signed integer type);
  • F.length >= 3;
  • and F[i] + F[i+1] = F[i+2] for all 0 <= i < F.length - 2.
    Also, 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 S, or return [] if it cannot be done.

Example 1:
Input: "123456579" ; Output: [123,456,579]
Example 2:
Input: "11235813" ; Output: [1,1,2,3,5,8,13]
Example 3:
Input: "112358130" ; Output: []
Explanation: The task is impossible.
Example 4:
Input: "0123" ; Output: []
Explanation: Leading zeroes are not allowed, so “01”, “2”, “3” is not valid.
Example 5:
Input: "1101111" ; Output: [110, 1, 111]
Explanation: The output [11, 0, 11, 11] would also be accepted.

Note:

  • 1 <= S.length <= 200
  • S contains only digits.

Solution : Backtracking

The key is handling corner cases:

  • Remove elements with leading zero
  • The element in the sequence should be at most Integer.MAX_VALUE
  • The sequence should has at least 3 elements
  • If current number is larger than the sum of previous two elements, stop backtracking
  • If we find a valid sequence, stop backtracking

Code:

public List<Integer> splitIntoFibonacci(String S) {
	List<Integer> res = new ArrayList<>();
	backtrack(S, res, 0);
	return res;
}

private boolean backtrack(String s, List<Integer> res, int index) {
	
	if (index == s.length() && res.size() >= 3) return true;
	
	for (int i = index; i < s.length(); i++) {
		// a number with leading 0s but not '0' is invalid
		if (i != index && s.charAt(index) == '0') break;
		
		long num = Long.parseLong(s.substring(index, i + 1));
		if (num > Integer.MAX_VALUE) break;
		
		int size = res.size();
		if (size >= 2 && num > res.get(size-1) + res.get(size-2)) break;
		
		if (size < 2 || num == res.get(size-1) + res.get(size-2)) {
		    res.add((int)num);
		    // If we find a valid sequence, stop backtracking
		    if (backtrack(s, res, i + 1)) return true;
		    res.remove(res.size() - 1);
		}
	}
	
	return false;
}

   Reprint policy


《Split Array into Fibonacci Sequence》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC