Skip to content

Commit 84b1f7d

Browse files
committed
feat: add solution 1977. Number of Ways to Separate Numbers
1 parent 99e5bc1 commit 84b1f7d

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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>&nbsp;</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>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= num.length &lt;= 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>&nbsp;</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>&nbsp;</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+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @param {string} num
3+
* @return {number}
4+
*/
5+
const numberOfCombinations = function (num) {
6+
if (num[0] === '0') return 0;
7+
8+
const MODULO = BigInt(10 ** 9 + 7);
9+
const n = num.length;
10+
const dp = Array.from({ length: n }, () => new Array(n + 1).fill(0n));
11+
const lcs = Array.from({ length: n + 1 }, () => new Array(n + 1).fill(0));
12+
13+
for (let a = n - 1; a >= 0; a--) {
14+
for (let b = a + 1; b < n; b++) {
15+
if (num[a] !== num[b]) continue;
16+
17+
lcs[a][b] = lcs[a + 1][b + 1] + 1;
18+
}
19+
}
20+
21+
for (let index = 0; index < n; index++) {
22+
for (let len = 1; len <= index + 1; len++) {
23+
const start = index - len + 1;
24+
25+
dp[index][len] += dp[index][len - 1];
26+
dp[index][len] %= MODULO;
27+
28+
if (num[start] === '0') continue;
29+
if (start === 0) {
30+
dp[index][len] += 1n;
31+
continue;
32+
}
33+
34+
if (start < len) {
35+
dp[index][len] += dp[start - 1][start];
36+
continue;
37+
}
38+
39+
const lc = lcs[start - len][start];
40+
41+
if (lc >= len || num[start - len + lc] <= num[start + lc]) {
42+
dp[index][len] += dp[start - 1][len];
43+
} else {
44+
dp[index][len] += dp[start - 1][len - 1];
45+
}
46+
}
47+
}
48+
49+
return Number(dp[n - 1][n] % MODULO);
50+
};

0 commit comments

Comments
 (0)