LeetCode 1297. Maximum Number of Occurrences of a Substring Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1297. Maximum Number of Occurrences of a Substring

Description

Given a string s, return the maximum number of occurrences of any substring under the following rules:

  • The number of unique characters in the substring must be less than or equal to maxLetters.
  • The substring size must be between minSize and maxSize inclusive.

 

Example 1:

Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 occurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).

Example 2:

Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.

 

Constraints:

  • 1 <= s.length <= 105
  • 1 <= maxLetters <= 26
  • 1 <= minSize <= maxSize <= min(26, s.length)
  • s consists of only lowercase English letters.

Solutions

Solution 1: Hash Table + Enumeration

According to the problem description, if a long string meets the condition, then its substring of length minSize must also meet the condition. Therefore, we only need to enumerate all substrings of length minSize in s, then use a hash table to record the occurrence frequency of all substrings, and find the maximum frequency as the answer.

The time complexity is O(n × m), and the space complexity is O(n × m). Here, n and m are the lengths of the string s and minSize, respectively. In this problem, m does not exceed 26.

PythonJavaC++GoTypeScriptRust
class Solution: def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int: ans = 0 cnt = Counter() for i in range(len(s) - minSize + 1): t = s[i : i + minSize] ss = set(t) if len(ss) <= maxLetters: cnt[t] += 1 ans = max(ans, cnt[t]) return ans(code-box)

Solution 2: Sliding Window + String Hashing

We can use a sliding window to maintain the number of distinct letters in the current substring, while using string hashing to efficiently calculate the hash value of substrings, thereby avoiding using strings as hash table keys and improving performance.

Specifically, we define a Hashing class to preprocess the prefix hash values and power values of the string s, so that we can calculate the hash value of any substring in O(1) time.

Then, we use a sliding window to traverse the string s, maintaining the number of distinct letters in the current window. When the window size reaches minSize, we calculate the hash value of the current substring and update its occurrence count. Next, we slide the window one position to the right and update the letter frequencies and the count of distinct letters.

The time complexity is O(n) and the space complexity is O(n), where n is the length of the string s.

PythonJavaC++GoTypeScriptRust
class Hashing: __slots__ = ["mod", "h", "p"] def __init__( self, s: Union[str, List[str]], base: int = 13331, mod: int = 998244353 ): self.mod = mod self.h = [0] * (len(s) + 1) self.p = [1] * (len(s) + 1) for i in range(1, len(s) + 1): self.h[i] = (self.h[i - 1] * base + ord(s[i - 1])) % mod self.p[i] = (self.p[i - 1] * base) % mod def query(self, l: int, r: int) -> int: return (self.h[r] - self.h[l - 1] * self.p[r - l + 1]) % self.mod class Solution: def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int: freq = Counter() hashing = Hashing(s) cnt = Counter() ans = k = 0 for i, c in enumerate(s, 1): freq[c] += 1 if freq[c] == 1: k += 1 if i >= minSize: if k <= maxLetters: x = hashing.query(i - minSize + 1, i) cnt[x] += 1 ans = max(ans, cnt[x]) j = i - minSize freq[s[j]] -= 1 if freq[s[j]] == 0: k -= 1 return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !