Problem solution · Java

Number of Ways to Separate Numbers

Number of Ways to Separate Numbers: a Java solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Dynamic programming
Source
walkccc LeetCode Solutions
Length
52 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Number of Ways to Separate Numbers, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 52 lines of Java from the credited upstream file 1977.java.
  • The implementation visibly relies on sequence storage, cached states.
  • 4 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeNumber of Ways to Separate Numbers · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int numberOfCombinations(String num) {    if (num.charAt(0) == '0')      return 0;     final int MOD = 1_000_000_007;    final int n = num.length();    // dp[i][k] := the number of possible lists of integers ending in num[i] with    // the length of the last number being 1..k    long[][] dp = new long[n][n + 1];    // lcs[i][j] := the number of the same digits in num[i..n) and num[j..n)    int[][] lcs = new int[n + 1][n + 1];     for (int i = n - 1; i >= 0; --i)      for (int j = i + 1; j < n; ++j)        if (num.charAt(i) == num.charAt(j))          lcs[i][j] = lcs[i + 1][j + 1] + 1;     for (int i = 0; i < n; ++i)      for (int k = 1; k <= i + 1; ++k) {        dp[i][k] += dp[i][k - 1];        dp[i][k] %= MOD;        // The last number is num[s..i].        final int s = i - k + 1;        if (num.charAt(s) == '0')          // the number of possible lists of integers ending in num[i] with the          // length of the last number being k          continue;        if (s == 0) {          // the whole string          dp[i][k] += 1;          continue;        }        if (s < k) {          // The length k is not enough, so add the number of possible lists of          // integers in num[0..s - 1].          dp[i][k] += dp[s - 1][s];          continue;        }        final int l = lcs[s - k][s];        if (l >= k || num.charAt(s - k + l) <= num.charAt(s + l))          // Have enough length k and num[s - k..s - 1] <= num[j..i].          dp[i][k] += dp[s - 1][k];        else          // Have enough length k but num[s - k..s - 1] > num[j..i].          dp[i][k] += dp[s - 1][k - 1];      }     return (int) dp[n - 1][n] % MOD;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗