<p>A teacher is writing a test with <code>n</code> true/false questions, with <code>&#39;T&#39;</code> denoting true and <code>&#39;F&#39;</code> denoting false. He wants to confuse the students by <strong>maximizing</strong> the number of <strong>consecutive</strong> questions with the <strong>same</strong> answer (multiple trues or multiple falses in a row).</p>

<p>You are given a string <code>answerKey</code>, where <code>answerKey[i]</code> is the original answer to the <code>i<sup>th</sup></code> question. In addition, you are given an integer <code>k</code>, the maximum number of times you may perform the following operation:</p>

<ul>
	<li>Change the answer key for any question to <code>&#39;T&#39;</code> or <code>&#39;F&#39;</code> (i.e., set <code>answerKey[i]</code> to <code>&#39;T&#39;</code> or <code>&#39;F&#39;</code>).</li>
</ul>

<p>Return <em>the <strong>maximum</strong> number of consecutive</em> <code>&#39;T&#39;</code>s or <code>&#39;F&#39;</code>s <em>in the answer key after performing the operation at most</em> <code>k</code> <em>times</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> answerKey = &quot;TTFF&quot;, k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can replace both the &#39;F&#39;s with &#39;T&#39;s to make answerKey = &quot;<u>TTTT</u>&quot;.
There are four consecutive &#39;T&#39;s.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> answerKey = &quot;TFFT&quot;, k = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can replace the first &#39;T&#39; with an &#39;F&#39; to make answerKey = &quot;<u>FFF</u>T&quot;.
Alternatively, we can replace the second &#39;T&#39; with an &#39;F&#39; to make answerKey = &quot;T<u>FFF</u>&quot;.
In both cases, there are three consecutive &#39;F&#39;s.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> answerKey = &quot;TTFTTFTT&quot;, k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can replace the first &#39;F&#39; to make answerKey = &quot;<u>TTTTT</u>FTT&quot;
Alternatively, we can replace the second &#39;F&#39; to make answerKey = &quot;TTF<u>TTTTT</u>&quot;. 
In both cases, there are five consecutive &#39;T&#39;s.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == answerKey.length</code></li>
	<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>answerKey[i]</code> is either <code>&#39;T&#39;</code> or <code>&#39;F&#39;</code></li>
	<li><code>1 &lt;= k &lt;= n</code></li>
</ul>

class Solution {
public:
    int m = 0;
    void lr_point0 (vector<int>& vc, int l, int r, int k, int& vs, int now) {
        m = max(m, now + k);
        if (r + 1 >= vs - 1)
        return ;

        if(vc[r + 1] <= k) {
            now += vc[r + 1] + vc[r + 2];
            k -= vc[r + 1];
            lr_point0(vc, l, r + 2, k, vs, now);
        }
        else if(r > l) { 
            k += vc[l + 1]; 
            now -= (vc[l] + vc[l + 1]); 
            lr_point0(vc, l + 2, r, k, vs, now);
        }
        else if(r == l){
            now = vc[r + 2];
            lr_point0(vc, l + 2, r + 2, k, vs, now);
        }
    }


    int maxConsecutiveAnswers(string answerKey, int k) {
        m = 0;
        int con = 0, ss = answerKey.size();
        char temp = answerKey[0];
        vector<int> vc;
        for (auto it : answerKey) {
            if(temp == it) {
                con ++;
            }
            else {
                vc.push_back(con);
                temp = it;
                con = 1;
            }
        }
        vc.push_back(con);

        int vs = vc.size(), ans = 0;
        if(vs == 1)
        return vc[0];
        lr_point0(vc, 0, 0, k ,vs, vc[0]);
        lr_point0(vc, 1, 1, k ,vs, vc[1]);
        return min(m, ss);
    }
};
15800