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>

from copy import deepcopy
class Solution:
    def maximumLengthSubstring(self, s: str) -> int:
        # my_dic = {}
    
        ss = set(s)
        print(len(ss))
        def d(ss):
            myd = {}
            for c in ss:
                myd[c] = []
            return myd
        my_dic = d(ss)

        i = 0
        # j = 0 + len(my_dic)j
        j = i + 1
        largest = 0
        while i < j:
            sub = s[i:j]
            check = checkStr(sub, deepcopy(my_dic))
            if check[0]:
                if j - i > largest:
                    largest = j - i
            else:
                j = i + check[2]
                i = i + check[1] + 1
                # j = i + len(my_dic)
                diff = j - i
                if diff < largest:
                    j = j + (largest - diff)
                continue
            j += 1
            if j > len(s):
                i += 1
                # j = i + len(my_dic)
                j = i + 1
                break
            # if len(s) -  i <= len(my_dic):
                # break




        return largest


def checkStr(s:str, d:dict) -> bool:
    for i, c in enumerate(s):
        d[c].append(i)
        val = d[c]
        if len(val) > 2:
            return [False, val[0], val[-1]]
    return [True, 0, 0]


        
179