Leetcode Problem 2168. Unique Substrings With Equal Digit Frequency
2168. Unique Substrings With Equal Digit Frequency
Leetcode Solutions
Python | Rolling Hash, Math, Set, Substring | Explanation
Initialize a set to store unique hashes of valid substrings.
Choose a prime number for the rolling hash algorithm.
Iterate over all possible starting points of substrings.
For each starting point, initialize variables to track the count of each digit, the number of unique digits, the maximum count of any digit, and the rolling hash.
Extend the substring one character at a time, updating the count of digits, the number of unique digits, the maximum count, and the rolling hash.
If the product of the number of unique digits and the maximum count equals the length of the current substring, add the hash to the set.
After iterating through all starting points and all possible substrings, return the size of the set as the number of unique substrings.