|
| 1 | +# [1977. Number of Ways to Separate Numbers](https://leetcode.com/problems/number-of-ways-to-separate-numbers) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +<div class="elfjS" data-track-load="description_content"><p>You wrote down many <strong>positive</strong> integers in a string called <code>num</code>. However, you realized that you forgot to add commas to seperate the different numbers. You remember that the list of integers was <strong>non-decreasing</strong> and that <strong>no</strong> integer had leading zeros.</p> |
| 6 | + |
| 7 | +<p>Return <em>the <strong>number of possible lists of integers</strong> that you could have written down to get the string </em><code>num</code>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<pre><strong>Input:</strong> num = "327" |
| 13 | +<strong>Output:</strong> 2 |
| 14 | +<strong>Explanation:</strong> You could have written down the numbers: |
| 15 | +3, 27 |
| 16 | +327 |
| 17 | +</pre> |
| 18 | + |
| 19 | +<p><strong class="example">Example 2:</strong></p> |
| 20 | + |
| 21 | +<pre><strong>Input:</strong> num = "094" |
| 22 | +<strong>Output:</strong> 0 |
| 23 | +<strong>Explanation:</strong> No numbers can have leading zeros and all numbers must be positive. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p><strong class="example">Example 3:</strong></p> |
| 27 | + |
| 28 | +<pre><strong>Input:</strong> num = "0" |
| 29 | +<strong>Output:</strong> 0 |
| 30 | +<strong>Explanation:</strong> No numbers can have leading zeros and all numbers must be positive. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p> </p> |
| 34 | +<p><strong>Constraints:</strong></p> |
| 35 | + |
| 36 | +<ul> |
| 37 | + <li><code>1 <= num.length <= 3500</code></li> |
| 38 | + <li><code>num</code> consists of digits <code>'0'</code> through <code>'9'</code>.</li> |
| 39 | +</ul> |
| 40 | +</div> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | + |
| 44 | +## Solutions |
| 45 | + |
| 46 | +**Solution: `Dynamic Programming`** |
| 47 | + |
| 48 | +- Time complexity: <em>O(n<sup>2</sup>)</em> |
| 49 | +- Space complexity: <em>O(n<sup>2</sup>)</em> |
| 50 | + |
| 51 | +<p> </p> |
| 52 | + |
| 53 | +### **JavaScript** |
| 54 | + |
| 55 | +```js |
| 56 | +/** |
| 57 | + * @param {string} num |
| 58 | + * @return {number} |
| 59 | + */ |
| 60 | +const numberOfCombinations = function (num) { |
| 61 | + if (num[0] === '0') return 0; |
| 62 | + |
| 63 | + const MODULO = BigInt(10 ** 9 + 7); |
| 64 | + const n = num.length; |
| 65 | + const dp = Array.from({ length: n }, () => new Array(n + 1).fill(0n)); |
| 66 | + const lcs = Array.from({ length: n + 1 }, () => new Array(n + 1).fill(0)); |
| 67 | + |
| 68 | + for (let a = n - 1; a >= 0; a--) { |
| 69 | + for (let b = a + 1; b < n; b++) { |
| 70 | + if (num[a] !== num[b]) continue; |
| 71 | + |
| 72 | + lcs[a][b] = lcs[a + 1][b + 1] + 1; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + for (let index = 0; index < n; index++) { |
| 77 | + for (let len = 1; len <= index + 1; len++) { |
| 78 | + const start = index - len + 1; |
| 79 | + |
| 80 | + dp[index][len] += dp[index][len - 1]; |
| 81 | + dp[index][len] %= MODULO; |
| 82 | + |
| 83 | + if (num[start] === '0') continue; |
| 84 | + if (start === 0) { |
| 85 | + dp[index][len] += 1n; |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + if (start < len) { |
| 90 | + dp[index][len] += dp[start - 1][start]; |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + const lc = lcs[start - len][start]; |
| 95 | + |
| 96 | + if (lc >= len || num[start - len + lc] <= num[start + lc]) { |
| 97 | + dp[index][len] += dp[start - 1][len]; |
| 98 | + } else { |
| 99 | + dp[index][len] += dp[start - 1][len - 1]; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return Number(dp[n - 1][n] % MODULO); |
| 105 | +}; |
| 106 | +``` |
0 commit comments