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

"""
1a1b1a1b1a
3a1b3a
Find each gap of 1 and then find largest letter after it.
If there isn't another occurrence after it then need to swap with end.
Can we just track left and right most occurence of letter?
"""


class Solution:
    def maxRepOpt1(self, text: str) -> int:
        compression = [[text[0], 1]]
        left = {text[0]: 0}
        right = {text[0]: 0}
        max_len = 1
        for i in range(1, len(text)):
            curr = text[i]
            if curr not in left:
                left[curr] = i
            right[curr] = i
            if curr == compression[-1][0]:
                compression[-1][1] += 1
            else:
                compression.append([curr, 1])
            max_len = max(max_len, compression[-1][1])

        print(compression)
        print(left)
        print(right)
        ind = 0
        for i in range(len(compression)):
            # at each compression check if there is a right most or left most that can make it longer
            # First check if you can grow it by 1
            ch, l = compression[i]

            if left[ch] != ind:
                max_len = max(max_len, l + 1)
            if right[ch] != ind + l - 1:
                max_len = max(max_len, l + 1)

            if (
                i != 0
                and i != len(compression) - 1
                and l == 1
                and compression[i - 1][0] == compression[i + 1][0]
            ):
                l_size = compression[i - 1][1]
                r_size = compression[i + 1][1]
                max_len = max(max_len, l_size + r_size)
                l_start = ind - l_size
                r_end = ind + r_size
                prev_ch = compression[i - 1][0]
                if left[prev_ch] != l_start:
                    max_len = max(max_len, l_size + r_size + 1)
                if right[prev_ch] != r_end:
                    max_len = max(max_len, l_size + r_size + 1)

            ind += l
        return max_len

18600