<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) {
    
    map<char,vector<pair<int,int>>>t;
    
    int st=0;

    for(int i=1;i<text.size();i++){
   
    if(text[i]!=text[i-1]){
  
    t[text[i-1]-'a'].push_back({st,i-1});
    
    st=i;}


    }

    int n=text.size();
    t[text[n-1]-'a'].push_back({st,n-1});
    
    int ans=0;
    for(int i=0;i<26;i++){
    if(t[i].size()>=2){
    ans=max(ans,t[i][0].second-t[i][0].first+2);
    }
    else if(t[i].size()!=0){
    ans=max(ans,t[i][0].second-t[i][0].first+1);
    }
    
    

    for(int j=1;j<t[i].size();j++){
    
    if(t[i].size()>=2){
    ans=max(ans,t[i][j].second-t[i][j].first+2);
    }
    else if(t[i].size()!=0){
    ans=max(ans,t[i][j].second-t[i][j].first+1);
    }
    
    if(t[i][j].first-t[i][j-1].second==2){

    if(t[i].size()>=3){
    ans=max(ans,t[i][j].second-t[i][j].first+1+t[i][j-1].second-t[i][j-1].first    +2);    
    }
    else{
    ans=max(ans,t[i][j].second-t[i][j].first+1+t[i][j-1].second-t[i][j-1].first+1);  

    }

    }

    }   


    }
     
return ans;

    }
};
10900