<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p>

<p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater than or equal to <code>k</code> characters, then reverse the first <code>k</code> characters and leave the other as original.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "abcdefg", k = 2
<strong>Output:</strong> "bacdfeg"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "abcd", k = 2
<strong>Output:</strong> "bacd"
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
	<li><code>s</code> consists of only lowercase English letters.</li>
	<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    string reverseStr(string s, int k) {
        int rev = 0;
        string ans = "", prev="";
        for(int i=0;i<s.length();i++){
            if(i%k == 0){
                if(!prev.empty()){
                    if(rev == 1){
                        for(int j = prev.length()-1;j>=0;j--)ans+=prev[j];
                    }
                    else for(int j=0;j<prev.length();j++)ans+=prev[j];
                }
                prev = "";
                rev^=1;
            }
            prev += s[i];
            
        }
        
        if(!prev.empty()){
            if(rev == 1){
                for(int j = prev.length()-1;j>=0;j--)ans+=prev[j];
            }
            else for(int j=0;j<prev.length();j++)ans+=prev[j];
        }
        return ans;
    }
};
9400