<p>You are given a string <code>text</code>. You can swap two of the characters in the <code>text</code>.</p>

<p>Return <em>the length of the longest substring with repeated characters</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> text = &quot;ababa&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can swap the first &#39;b&#39; with the last &#39;a&#39;, or the last &#39;b&#39; with the first &#39;a&#39;. Then, the longest repeated character substring is &quot;aaa&quot; with length 3.
</pre>

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

<pre>
<strong>Input:</strong> text = &quot;aaabaaa&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> Swap &#39;b&#39; with the last &#39;a&#39; (or the first &#39;a&#39;), and we get longest repeated character substring &quot;aaaaaa&quot; with length 6.
</pre>

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

<pre>
<strong>Input:</strong> text = &quot;aaaaa&quot;
<strong>Output:</strong> 5
<strong>Explanation:</strong> No need to swap, longest repeated character substring is &quot;aaaaa&quot; with length is 5.
</pre>

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

<ul>
	<li><code>1 &lt;= text.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>text</code> consist of lowercase English characters only.</li>
</ul>

class Solution:
    def maxRepOpt1(self, text: str) -> int:
        dicto={}
        count2=1
        count1=0
        maxout=0
        maxout2=0
        c=text[0]
        cc=text[0]
        ccc=text[0]
        dicto[text[0]]=1
        count1=1
        flag=False
        pec=0
        for i in range(1,len(text)):
            dicto[text[i]]=dicto.get(text[i],0)+1
        print(dicto)
        for i in range(1,len(text)):
            
            print("counts=",i, count1,count2)
            if c!=text[i]:
                
                if flag:
                    if maxout<count2+count1+1 and count2+count1+1<=dicto[c] and c==ccc:
                        maxout=count2+count1+1
                        count2=count1
                        
                    else:
                        if maxout<count2+count1 and c==ccc:
                            maxout=count2+count1
                            count2=count1
                            
                    
                if maxout2<=count1:
                    if (dicto[c]>count1):
                        maxout2=count1+1
                        
                    else:
                        maxout2=count1
                        
                    
                if count1==1 :
                    if pec==0:
                        flag=True
                        count1=1
                        pec=1
                    else:
                        pec=0
                        flag=True
                        count2=count1
                        count1=1
                        
                else:
                    flag=False
                    count2=count1
                    count1=1
                    pec=0
                ccc=cc
                cc=c
                c=text[i]
            else:
                count1+=1
            print(maxout,maxout2)
        if flag:
            if maxout<count2+count1+1 and count2+count1+1<=dicto[c] and c==ccc:
                maxout=count2+count1+1
            else:
                if maxout<count2+count1 and c==ccc:
                    maxout=count2+count1
            
        
        if maxout2<=count1:
                    if (dicto[c]>count1):
                        maxout2=count1+1
                        
                    else:
                        maxout2=count1
        
        return max(maxout,maxout2)

                


17100