You wrote down many positive integers in a string called num. However, you realized that you forgot to add commas to seperate the different numbers.
You remember that the list of integers was non-decreasing and that no integer had leading zeros.
Return _thenumber of possible lists of integers that you could have written down to get the string _num. Since the answer may be large, return it modulo10^9 + 7.
We want to split the string into non-decreasing positive integers with no leading zeros. For each position, we can try all possible previous splits and check if the new number is at least as large as the previous. To compare large numbers efficiently, we use precomputed LCP (longest common prefix) arrays.
classSolution {
public:int numberOfCombinations(string num) {
int n = num.size(), mod =1e9+7;
if (num[0] =='0') return0;
vector<vector<int>> lcp(n+1, vector<int>(n+1));
for (int i = n-1; i >=0; --i)
for (int j = n-1; j >=0; --j)
lcp[i][j] = (num[i] == num[j] ? lcp[i+1][j+1]+1:0);
vector<vector<int>> dp(n+1, vector<int>(n+1));
for (int i =1; i <= n; ++i) {
for (int k =1; k <= i; ++k) {
if (num[i-k] =='0') continue;
if (i-k ==0) { dp[i][k] =1; continue; }
int j = i-k;
if (j-k >=0) {
int cmp = lcp[j-k][i-k];
if (cmp >= k || num[j-k+cmp] <= num[i-k+cmp])
dp[i][k] = (dp[i][k] + dp[j][k]) % mod;
}
for (int t =1; t < k && j-t >=0; ++t)
dp[i][k] = (dp[i][k] + dp[j][t]) % mod;
}
}
int ans =0;
for (int k =1; k <= n; ++k) ans = (ans + dp[n][k]) % mod;
return ans;
}
};
classSolution {
publicintnumberOfCombinations(String num) {
int n = num.length(), mod = 1_000_000_007;
if (num.charAt(0) =='0') return 0;
int[][] lcp =newint[n+1][n+1];
for (int i = n-1; i >= 0; i--)
for (int j = n-1; j >= 0; j--)
lcp[i][j]= (num.charAt(i) == num.charAt(j) ? lcp[i+1][j+1]+1 : 0);
int[][] dp =newint[n+1][n+1];
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= i; k++) {
if (num.charAt(i-k) =='0') continue;
if (i-k == 0) { dp[i][k]= 1; continue; }
int j = i-k;
if (j-k >= 0) {
int cmp = lcp[j-k][i-k];
if (cmp >= k || num.charAt(j-k+cmp) <= num.charAt(i-k+cmp))
dp[i][k]= (dp[i][k]+ dp[j][k]) % mod;
}
for (int t = 1; t < k && j-t >= 0; t++)
dp[i][k]= (dp[i][k]+ dp[j][t]) % mod;
}
}
int ans = 0;
for (int k = 1; k <= n; k++) ans = (ans + dp[n][k]) % mod;
return ans;
}
}
classSolution {
funnumberOfCombinations(num: String): Int {
val n = num.length
val mod = 1_000_000_007
if (num[0] =='0') return0val lcp = Array(n+1) { IntArray(n+1) }
for (i in n-1 downTo 0) for (j in n-1 downTo 0)
lcp[i][j] = if (num[i] == num[j]) lcp[i+1][j+1]+1else0val dp = Array(n+1) { IntArray(n+1) }
for (i in1..n) {
for (k in1..i) {
if (num[i-k] =='0') continueif (i-k ==0) { dp[i][k] = 1; continue }
val j = i-k
if (j-k >=0) {
val cmp = lcp[j-k][i-k]
if (cmp >= k || num[j-k+cmp] <= num[i-k+cmp])
dp[i][k] = (dp[i][k] + dp[j][k]) % mod
}
for (t in1 until k) if (j-t >=0)
dp[i][k] = (dp[i][k] + dp[j][t]) % mod
}
}
var ans = 0for (k in1..n) ans = (ans + dp[n][k]) % mod
return ans
}
}
classSolution:
defnumberOfCombinations(self, num: str) -> int:
n, mod = len(num), 10**9+7if num[0] =='0': return0 lcp = [[0]*(n+1) for _ in range(n+1)]
for i in range(n-1, -1, -1):
for j in range(n-1, -1, -1):
if num[i] == num[j]:
lcp[i][j] = lcp[i+1][j+1] +1 dp = [[0]*(n+1) for _ in range(n+1)]
for i in range(1, n+1):
for k in range(1, i+1):
if num[i-k] =='0': continueif i-k ==0:
dp[i][k] =1continue j = i-k
if j-k >=0:
cmp = lcp[j-k][i-k]
if cmp >= k or num[j-k+cmp] <= num[i-k+cmp]:
dp[i][k] = (dp[i][k] + dp[j][k]) % mod
for t in range(1, k):
if j-t >=0:
dp[i][k] = (dp[i][k] + dp[j][t]) % mod
return sum(dp[n][k] for k in range(1, n+1)) % mod
impl Solution {
pubfnnumber_of_combinations(num: String) -> i32 {
let n = num.len();
let modv =1_000_000_007;
let num = num.as_bytes();
if num[0] ==b'0' { return0; }
letmut lcp =vec![vec![0; n+1]; n+1];
for i in (0..n).rev() {
for j in (0..n).rev() {
if num[i] == num[j] {
lcp[i][j] = lcp[i+1][j+1] +1;
}
}
}
letmut dp =vec![vec![0; n+1]; n+1];
for i in1..=n {
for k in1..=i {
if num[i-k] ==b'0' { continue; }
if i-k ==0 { dp[i][k] =1; continue; }
let j = i-k;
if j >= k {
let cmp = lcp[j-k][i-k];
if cmp >= k || num[j-k+cmp] <= num[i-k+cmp] {
dp[i][k] = (dp[i][k] + dp[j][k]) % modv;
}
}
for t in1..k {
if j >= t {
dp[i][k] = (dp[i][k] + dp[j][t]) % modv;
}
}
}
}
letmut ans =0;
for k in1..=n { ans = (ans + dp[n][k]) % modv; }
ans
}
}