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

        
        
        def find_longest1(nums):
            # function getting an ordered array. What is the longest series of consecutive numbers with a single gap?
            # if existend, is there an entry not belonging to it? If yes, return end entry - start entry
            #                                                     If no, return end entry - start entry -1
            # if non existent, what is the longest consecutive number series?
            # if there exists an entry outside, return leng + 1, else, len
            longest_with_gap = 0
            current_with_gap = 0
            longest_no_gap = 1
            current_no_gap = 1
            last_i = nums[0]
            for i in nums[1:]:
                if last_i == i - 1:
                    current_no_gap +=1
                    if current_with_gap:
                        current_with_gap += 1
                elif last_i == i - 2:
                    current_with_gap = current_no_gap + 1
                    current_no_gap = 1
                else:
                    current_no_gap = 1
                    current_with_gap = 0
                longest_with_gap = max(longest_with_gap, current_with_gap)
                longest_no_gap = max(longest_no_gap, current_no_gap)
                last_i = i
            return max(min(len(nums), longest_with_gap + 1), min(len(nums), longest_no_gap + 1))
        
        dicti = {c: [] for c in text}
        for i,c in enumerate(text):
            dicti[c].append(i)
        res = 0
        for listi in dicti.values():
            res = max(res, find_longest1(listi))
        
        return res
17500