<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) {
        string cpy=text;
        bool swapc=0;
        int n=text.length();
        int res=0;
        unordered_map<char,vector<int>> ump;
        for(int i=0;i<n;i++){
            if(ump.find(text[i])!=ump.end()){
                if(ump[text[i]].size()==1)
                    ump[text[i]].push_back(i);
                else
                    ump[text[i]][1]=i;
            }
            else
                ump[text[i]].push_back(i);
        }
        char cur=text[0];
        int left=0;
        int right=0;
        while(right<n){
            if(cpy[right]!=cur && (swapc==true || ump[cur].size()==1 ||(ump[cur].size()>1 && 
              ump[cur][0]>=left && ump[cur][0]<right && ump[cur][1]>=left && ump[cur][1]<right))){
                res=max(res,right-left);
                //cout<<"before  "<<left<<"  "<<right<<"  "<<cur<<endl;
                cpy=text;
                while(left<=right && cpy[left]==cur){
                    left++;
                }
                right=left;
                //cout<<"after  "<<left<<"  "<<right<<"  "<<cpy[right]<<endl;
                cur=cpy[right];
                swapc=false;
            }
            else{
                if(cpy[right]==cur)
                    right++;
                else{
                    if(swapc==false){
                        if(ump[cur][0]<left){
                            swap(cpy[right],cpy[ump[cur][0]]);
                            swapc=true;
                        }
                        else if(ump[cur][1]>right){
                            swap(cpy[right],cpy[ump[cur][1]]);
                            swapc=true;
                        }
                    }
                }
            }
        }
        if(right-left>=res){
            res=max(res,right-left);
            if( swapc==false && ump[cpy[left]][0]<left){
                res++;
            }
        }
        
        return res;

    }
};
9700