# Solving Leetcode Interviews in Seconds with AI: Maximum Number of Occurrences of a Substring


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "1297" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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.  

	# Explanation
	Here's a breakdown of the approach, followed by the Python code:

*   **Focus on `minSize`:** Since we are looking for the *maximum* number of occurrences, and the substring length must be *between* `minSize` and `maxSize`, we can optimize by *only* considering substrings of length `minSize`. Any substring longer than `minSize` that satisfies the unique character constraint *must* contain a substring of length `minSize` that *also* satisfies the constraint. If the longer string occurs more often than its shorter substrings, then the shorter substrings are less important.

*   **Sliding Window and Frequency Counting:** Iterate through the string using a sliding window of size `minSize`.  For each substring, check if the number of unique characters is within the allowed limit (`maxLetters`). If it is, update the frequency count of that substring.

*   **Maximize Frequency:**  After iterating through all possible substrings, find the substring with the maximum frequency.

*   **Time & Space Complexity:** O(n), O(n) where n is the length of string s.

	
	# Code
	```python
	def maxRepOpt1(s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
    """
    Finds the maximum number of occurrences of any substring under given rules.
    """
    n = len(s)
    substring_counts = {}

    for i in range(n - minSize + 1):
        substring = s[i:i + minSize]
        unique_chars = set(substring)

        if len(unique_chars) <= maxLetters:
            substring_counts[substring] = substring_counts.get(substring, 0) + 1

    max_count = 0
    for count in substring_counts.values():
        max_count = max(max_count, count)

    return max_count
	```
			
