<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>

'''
we either already have greatest run 
or we are combining 2 runs together

if 2 runs are seperated by 1 character
we can either connect them by moving a letter from one of the runs
res = run1 + run2

or from a different run of that letter
res = run1 + 1 + run2

we can see if run1 + run2 < total count of that letter to determine if its possible to do that one
'''
class Solution:
    def maxRepOpt1(self, text: str) -> int:
        runs = []
        counter = Counter(text)

        lastChar = ""
        run = 0
        res = 0

        for c in text:
            if c == lastChar:
                run += 1
            else:
                if run:
                    res = max(res, run)
                    runs.append((run, lastChar))
                run = 1
                lastChar = c

        if run:
            res = max(res, run)
            runs.append((run, lastChar))

        print(runs)
        for i in range(len(runs) - 2):
            if runs[i][1] != runs[i+2][1]:
                continue

            if runs[i+1][0] == 1:
                if runs[i][0] + runs[i+2][0] < counter[runs[i][1]]:
                    res = max(res, runs[i][0] + runs[i+2][0] + 1)
                else:
                    res = max(res, runs[i][0] + runs[i+2][0])
            else:
                res = max(res, max(runs[i][0], runs[i+2][0]) + 1)
            
        for run in runs:
            cnt, c = run
            if cnt < counter[c]:
                res = max(res, cnt + 1)
        return res

18300