Given a string <code>s</code>, return the <strong>maximum</strong> length of a <span data-keyword="substring">substring</span>&nbsp;such that it contains <em>at most two occurrences</em> of each character.
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;bcbbbcba&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">4</span></p>

<p><strong>Explanation:</strong></p>
The following substring has a length of 4 and contains at most two occurrences of each character: <code>&quot;bcbb<u>bcba</u>&quot;</code>.</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;aaaa&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>
The following substring has a length of 2 and contains at most two occurrences of each character: <code>&quot;<u>aa</u>aa&quot;</code>.</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= s.length &lt;= 100</code></li>
	<li><code>s</code> consists only of lowercase English letters.</li>
</ul>

"""
Given a string 
s
, return the 
maximum
 length of a 
substring
 such that it contains 
at most two occurrences
 of each character.
"""
class Solution:
    def maximumLengthSubstring(self, s: str) -> int:
        n = len(s)
        max_length = 0

        # Check all substrings and count the frequency of each character
        for start in range(n):
            for end in range(start + 1, n + 1):
                substring = s[start:end]
                if self.isValidSubstring(substring):
                    max_length = max(max_length, len(substring))

        return max_length

    # Check if the frequency of each character is at most 2
    def isValidSubstring(self, substring):
        freq = [0] * 26
        for char in substring:
            freq[ord(char) - ord('a')] += 1
            if freq[ord(char) - ord('a')] > 2:
                return False
        return True
509