Split Array into Fibonacci Sequence

medium backtracking string

Problem

Given a string of digits, return any split of length >= 3 such that each value (no leading zeros, fits in 32-bit int) equals the sum of the previous two. Return [] if impossible.

Inputnum = "1101111"
Output[11,0,11,11]
One valid split where each number = sum of previous two.

def splitIntoFibonacci(num):
    n = len(num)
    res = []
    def backtrack(start, path):
        if start == n and len(path) >= 3:
            res.extend(path); return True
        for end in range(start + 1, n + 1):
            piece = num[start:end]
            if len(piece) > 1 and piece[0] == "0": break
            val = int(piece)
            if val > 2**31 - 1: break
            if len(path) < 2 or path[-1] + path[-2] == val:
                if backtrack(end, path + [val]): return True
        return False
    backtrack(0, [])
    return res
function splitIntoFibonacci(num) {
  const n = num.length;
  const res = [];
  const backtrack = (start, path) => {
    if (start === n && path.length >= 3) { res.push(...path); return true; }
    for (let end = start + 1; end <= n; end++) {
      const piece = num.slice(start, end);
      if (piece.length > 1 && piece[0] === "0") break;
      const val = Number(piece);
      if (val > 2147483647) break;
      if (path.length < 2 || path[path.length - 1] + path[path.length - 2] === val) {
        path.push(val);
        if (backtrack(end, path)) return true;
        path.pop();
      }
    }
    return false;
  };
  backtrack(0, []);
  return res;
}
import java.util.*;
class Solution {
  public List<Integer> splitIntoFibonacci(String num) {
    List<Integer> res = new ArrayList<>();
    backtrack(num, 0, res);
    return res;
  }
  boolean backtrack(String num, int start, List<Integer> path) {
    if (start == num.length() && path.size() >= 3) return true;
    for (int end = start + 1; end <= num.length(); end++) {
      String piece = num.substring(start, end);
      if (piece.length() > 1 && piece.charAt(0) == '0') break;
      long val = Long.parseLong(piece);
      if (val > Integer.MAX_VALUE) break;
      int s = path.size();
      if (s < 2 || path.get(s - 1) + path.get(s - 2) == val) {
        path.add((int) val);
        if (backtrack(num, end, path)) return true;
        path.remove(path.size() - 1);
      }
    }
    return false;
  }
}
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    bool backtrack(const string &num, int start, vector<int> &path) {
        if (start == (int)num.size() && path.size() >= 3) return true;
        for (int end = start + 1; end <= (int)num.size(); end++) {
            string piece = num.substr(start, end - start);
            if (piece.size() > 1 && piece[0] == '0') break;
            long long val = stoll(piece);
            if (val > INT_MAX) break;
            int s = path.size();
            if (s < 2 || (long long)path[s - 1] + path[s - 2] == val) {
                path.push_back((int)val);
                if (backtrack(num, end, path)) return true;
                path.pop_back();
            }
        }
        return false;
    }
    vector<int> splitIntoFibonacci(string num) {
        vector<int> res;
        backtrack(num, 0, res);
        return res;
    }
};
Time: O(n^3) worst case Space: O(n)