Problem solution · Java

String Transformation

String Transformation: 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
78 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 String Transformation, 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

  • 78 lines of Java from the credited upstream file 2851.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 codeString Transformation · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  // This dynamic programming table dp[k][i] represents the number of ways to  // rearrange the String s after k steps such that it starts with s[i].  // A String can be rotated from 1 to n - 1 times. The transition rule is  // dp[k][i] = sum(dp[k - 1][j]) for all j != i. For example, when n = 4 and  // k = 3, the table looks like this:  //  // -----------------------------------------------------------  // |       | i = 0 | i = 1 | i = 2 | i = 3 | sum = (n - 1)^k |  // -----------------------------------------------------------  // | k = 0 |   1   |   0   |   0   |   0   |        1        |  // | k = 1 |   0   |   1   |   1   |   1   |        3        |  // | k = 2 |   3   |   2   |   2   |   2   |        9        |  // | k = 3 |   6   |   7   |   7   |   7   |       27        |  // -----------------------------------------------------------  //  // By observation, we have  //   * dp[k][!0] = ((n - 1)^k - (-1)^k) / n  //   * dp[k][0] = dp[k][!0] + (-1)^k  public int numberOfWays(String s, String t, long k) {    final int n = s.length();    final int negOnePowK = (k % 2 == 0 ? 1 : -1); // (-1)^k    final int[] z = zFunction(s + t + t);    final List<Integer> indices = getIndices(z, n);    int[] dp = new int[2]; // dp[0] := dp[k][0]; dp[1] := dp[k][!0]    dp[1] = (int) ((modPow(n - 1, k) - negOnePowK + MOD) % MOD * modPow(n, MOD - 2) % MOD);    dp[0] = (int) ((dp[1] + negOnePowK + MOD) % MOD);    int ans = 0;    for (final int index : getIndices(z, n)) {      ans += dp[index == 0 ? 0 : 1];      ans %= MOD;    }    return ans;  }   private static final int MOD = 1_000_000_007;   private long modPow(long x, long n) {    if (n == 0)      return 1;    if (n % 2 == 1)      return x * modPow(x, n - 1) % MOD;    return modPow(x * x % MOD, n / 2);  }   // Returns the z array, where z[i] is the length of the longest prefix of  // s[i..n) which is also a prefix of s.  //  // https://cp-algorithms.com/string/z-function.html#implementation  private int[] zFunction(final String s) {    final int n = s.length();    int[] z = new int[n];    int l = 0;    int r = 0;    for (int i = 1; i < n; ++i) {      if (i < r)        z[i] = Math.min(r - i, z[i - l]);      while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i]))        ++z[i];      if (i + z[i] > r) {        l = i;        r = i + z[i];      }    }    return z;  }   // Returns the indices in `s` s.t. for each `i` in the returned indices,  // `s[i..n) + s[0..i) = t`.  private List<Integer> getIndices(int[] z, int n) {    List<Integer> indices = new ArrayList<>();    for (int i = n; i < n + n; ++i)      if (z[i] >= n)        indices.add(i - n);    return indices;  }} 

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 ↗