Talentd
FleetCode

1977. Number of Ways to Separate Numbers

Hard21.7% acceptance1 company

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 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.

Example 1:

Input: num = "327"
Output: 2
Explanation: You could have written down the numbers:
3, 27
327

Example 2:

Input: num = "094"
Output: 0
Explanation: No numbers can have leading zeros and all numbers must be positive.

Example 3:

Input: num = "0"
Output: 0
Explanation: No numbers can have leading zeros and all numbers must be positive.

Constraints:

  • 1 <= num.length <= 3500
  • num consists of digits '0' through '9'.

Asked at 1 company

Hints open one at a time

Hint 1
If we know the current number has d digits, how many digits can the previous number have?
Hint 2
Is there a quick way of calculating the number of possibilities for the previous number if we know that it must have less than or equal to d digits? Try to do some pre-processing.
44:19
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def numberOfCombinations(self, num: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"327"
Input
"094"
Input
"0"
Your code is saved on this device.Solution