Skip to main content
Back to problems
#842
Medium Algorithms

Split array into fibonacci sequence

String Backtracking
40.2% acceptance
Feb 22, 2026
1180
309
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: 0 <= f[i] < 231, (that is, each integer fits in 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. 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.

Solution

Rust
Time O(2^n)
Space O(n)
LeetCode
solution.rs
/*
 * 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:
 * 0 <= f[i] < 231, (that is, each integer fits in 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.
 * 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]
 * Explanation: The output [110, 1, 111] would also be accepted.
 * Example 2:
 * Input: num = "112358130"
 * Output: []
 * Explanation: The task is impossible.
 * Example 3:
 * Input: num = "0123"
 * Output: []
 * Explanation: Leading zeroes are not allowed, so "01", "2", "3" is not valid.
 * Constraints:
 * 1 <= num.length <= 200
 * num contains only digits.
 */

impl Solution {
  pub fn split_into_fibonacci(num: String) -> Vec<i32> {
    let digits: Vec<u8> = num.bytes().map(|b| b - b'0').collect();
    let _n = digits.len();
    fn parse(digits: &[u8], start: usize, end: usize) -> Option<i64> {
      if end <= start { return None; }
      if end - start > 1 && digits[start] == 0 { return None; }
      let mut v: i64 = 0;
      for i in start..end {
        v = v * 10 + digits[i] as i64;
        if v > i32::MAX as i64 { return None; }
      }
      Some(v)
    }
    fn backtrack(digits: &[u8], pos: usize, seq: &mut Vec<i32>) -> bool {
      let n = digits.len();
      if pos == n && seq.len() >= 3 { return true; }
      for end in pos+1..=n {
        let v = parse(digits, pos, end);
        if v.is_none() { break; }
        let v = v.unwrap() as i32;
        let m = seq.len();
        if m >= 2 {
          let expected = seq[m-1] as i64 + seq[m-2] as i64;
          if v as i64 > expected { break; }
          if (v as i64) < expected { continue; }
        }
        seq.push(v);
        if backtrack(digits, end, seq) { return true; }
        seq.pop();
      }
      false
    }
    let mut seq = vec![];
    backtrack(&digits, 0, &mut seq);
    seq
  }
}