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>

class Solution:
    def maximumLengthSubstring(self, s: str) -> int:
        l = len(s)
        cumulative_letters_counts = [[0 for i in range(26)] for j in range(l + 1)]
        for i in range(1, l + 1):
            cumulative_letters_counts[i] = cumulative_letters_counts[i - 1].copy()
            cumulative_letters_counts[i][ord(s[i - 1]) - ord('a')] += 1
        res = 2
        for i in range(l + 1):
            for j in range(i+1, l + 1):
                valid = True
                for k in range(26):
                    if cumulative_letters_counts[j][k] - cumulative_letters_counts[i][k] > 2:
                        valid = False
                        break
                if valid:
                    res = max(res, j - i)
        return res


383