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. 1 <= S.length <= 200
  2. S contains only digits.

Rust Solution

struct Solution;

impl Solution {
    fn split_into_fibonacci(s: String) -> Vec<i32> {
        let mut cur = vec![];
        let mut res = vec![];
        Self::dfs(0, &mut cur, &mut res, &s, s.len());
        res
    }
    fn dfs(start: usize, cur: &mut Vec<i32>, res: &mut Vec<i32>, s: &str, end: usize) {
        if start == end && cur.len() > 2 {
            *res = cur.to_vec();
        } else {
            for i in 1..=(end - start) {
                let t = &s[start..start + i];
                if &s[start..start + 1] == "0" && i > 1 {
                    break;
                }
                if let Ok(x) = t.parse::<i32>() {
                    let n = cur.len();
                    if n < 2 {
                        cur.push(x);
                        Self::dfs(start + i, cur, res, s, end);
                        cur.pop();
                    } else {
                        if x > cur[n - 1] + cur[n - 2] {
                            break;
                        }
                        if x == cur[n - 1] + cur[n - 2] {
                            cur.push(x);
                            Self::dfs(start + i, cur, res, s, end);
                            cur.pop();
                        }
                    }
                } else {
                    break;
                }
            }
        }
    }
}

#[test]
fn test() {
    let s = "123456579".to_string();
    let res = vec![123, 456, 579];
    assert_eq!(Solution::split_into_fibonacci(s), res);
    let s = "11235813".to_string();
    let res = vec![1, 1, 2, 3, 5, 8, 13];
    assert_eq!(Solution::split_into_fibonacci(s), res);
    let s = "112358130".to_string();
    let res: Vec<i32> = vec![];
    assert_eq!(Solution::split_into_fibonacci(s), res);
    let s = "0123".to_string();
    let res: Vec<i32> = vec![];
    assert_eq!(Solution::split_into_fibonacci(s), res);
    let s = "1101111".to_string();
    let res = vec![110, 1, 111];
    assert_eq!(Solution::split_into_fibonacci(s), res);
}

Having problems with this solution? Click here to submit an issue on github.