Number of Ways to Separate Numbers - Problem

You wrote down many positive integers in a string called num. However, you realized that you forgot to add commas to separate the different numbers. You remember that the list of integers was non-decreasing and that no integer had leading zeros.

Return the number of possible lists of integers that you could have written down to get the string num. Since the answer may be large, return it modulo 109 + 7.

Input & Output

Example 1 — Basic Case
$ Input: num = "327"
Output: 2
💡 Note: Two valid ways: [3, 27] since 3 ≤ 27, and [327] as single number. Cannot use [32, 7] because 32 > 7.
Example 2 — Single Digit
$ Input: num = "9"
Output: 1
💡 Note: Only one way: [9] as single number.
Example 3 — Leading Zeros
$ Input: num = "094"
Output: 0
💡 Note: No valid splits because numbers cannot have leading zeros. "094", "09", "04", "0" all have leading zeros.

Constraints

  • 1 ≤ num.length ≤ 3500
  • num consists of digits only

Visualization

Tap to expand
Number of Ways to Separate "327""327"Input: String of digitsValid Splits:[3, 27]3 ≤ 27 ✓[327]Single number ✓Invalid Split:[32, 7]32 > 7 ✗
Understanding the Visualization
1
Input
String of digits without separators
2
Process
Find all ways to split into non-decreasing numbers
3
Output
Count of valid splitting methods
Key Takeaway
🎯 Key Insight: Use dynamic programming to systematically count all valid non-decreasing splits while avoiding leading zeros
Asked in
Google 15 Facebook 12
18.5K Views
Medium Frequency
~35 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen