<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 {
public:
    int maxRepOpt1(string text) {
        vector<pair<char, int>> running;
        // generate a running-length encoded text
        for (char ch : text) {
            if (running.empty() || running.back().first != ch) {
                running.push_back(make_pair(ch, 0));
            }
            running.back().second++;
        }
        int max_len = 0;
        for (int i = 0; i < running.size(); ++i) {
            // find if there exists another block with this char
            bool has_other = false;
            for (int j = 0; j < running.size(); ++j) {
                if (j == i) {
                    continue;
                }
                if (running[j].first == running[i].first) {
                    has_other = true;
                    break;
                }
            }
            max_len = max(max_len, running[i].second + has_other);
            // we don't need to check the following at the edge of text
            if (i == 0 || i == running.size() - 1) {
                continue;
            }
            // check for aaaa...aba...aaaa
            if (running[i].second == 1) {
                int left = running[i-1].second;
                int right = running[i+1].second;
                has_other = false;
                if (running[i-1].first != running[i+1].first) {
                    continue;
                }
                for (int j = 0; j < running.size(); ++j) {
                    if (j == i - 1 || j == i + 1) {
                        continue;
                    }
                    if (running[j].first == running[i-1].first) {
                        has_other = true;
                        break;
                    }
                }
                max_len = max(max_len, left + right + has_other);
            }
        }
        return max_len;
    }
};
9200