Problem solution · Java

Unique Substrings With Equal Digit Frequency

Unique Substrings With Equal Digit Frequency: a Java solution using hash-based lookup. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Hash-based lookup
Source
walkccc LeetCode Solutions
Length
63 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

Hash-based lookup

For Unique Substrings With Equal Digit Frequency, the implementation stores previously seen values or frequencies in a hash table for direct membership and lookup operations.

  1. Decide the key that represents the information needed later.
  2. Update its count or stored state while scanning the input.
  3. Use constant-time expected lookups to detect matches or assemble the result.

Code notes

  • 63 lines of Java from the credited upstream file 2168.java.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
  • 5 loop blocks detected.

Complexity

Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.

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 codeUnique Substrings With Equal Digit Frequency · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int equalDigitFrequency(String s) {    final int n = s.length();    int[][] counts = new int[n][]; // counts[i] := the counter map of s[0..i]    int[] count = new int[10];    long[] pows = new long[n + 1]; // pows[i] := BASE^i % HASH    // hash[i] = the hash of the first i letters of s, where hash[i] =    // (26^(i - 1) * s[0] + 26^(i - 2) * s[1] + ... + s[i - 1]) % HASH    long[] hash = new long[n + 1];    Set<Integer> seen = new HashSet<>();     pows[0] = 1;     for (int i = 0; i < n; ++i) {      ++count[s.charAt(i) - '0'];      counts[i] = count.clone();      pows[i + 1] = pows[i] * BASE % HASH;      hash[i + 1] = (hash[i] * BASE + val(s.charAt(i))) % HASH;    }     for (int i = 0; i < n; ++i)      for (int j = i; j < n; ++j)        if (isSameFreq(counts, i, j))          seen.add(getRollingHash(i, j + 1, hash, pows));     return seen.size();  }   private static final int MAX = 1001;  private static final int BASE = 11;  private static final int HASH = 1_000_000_007;   private static int val(char c) {    return c - '0' + 1;  }   // Returns true if s[i..j] has the same digit frequency.j  private boolean isSameFreq(int[][] counts, int i, int j) {    int[] count = counts[j].clone();    if (i > 0)      for (int num = 0; num < 10; ++num)        count[num] -= counts[i - 1][num];    return equalFreq(count);  }   private boolean equalFreq(int[] count) {    int minfreq = MAX;    int maxfreq = 0;    for (final int freq : count)      if (freq > 0) {        minfreq = Math.min(minfreq, freq);        maxfreq = Math.max(maxfreq, freq);      }    return minfreq == maxfreq;  }   // Returns the rolling hash of s[l..r).  private int getRollingHash(int l, int r, long[] hash, long[] pows) {    final long h = (hash[r] - hash[l] * pows[r - l]) % HASH;    return (int) (h < 0 ? h + HASH : h);  }} 

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 ↗