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

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

&nbsp;
Example 1:
Input: s = "abcdefg", k = 2
Output: "bacdfeg"
Example 2:
Input: s = "abcd", k = 2
Output: "bacd"

&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 104
	s consists of only lowercase English letters.
	1 &lt;= k &lt;= 104


class Solution:
    def reverseStr(self, s: str, k: int) -> str:
        a=list(s)
        for i in range(0,len(a),2*k):
            a[i:i+k]=a[i:i+k][::-1]
        print(a)
        return("".join(a))
class Solution {
    public String reverseStr(String s, int k) {
        char[] ch=s.toCharArray();
        int cnt=1,i=0;
        StringBuilder sb=new StringBuilder();
        String ans="";
        if(k>=s.length()){
            sb.append(s);
            sb.reverse();
            return sb.toString();
        }
        for(i=0;i<s.length()-k;i+=k){
            String str=s.substring(i,i+k);
            if(cnt%2!=0){
                sb.append(str);
                sb.reverse();
                ans+=sb.toString();
                cnt++;
                sb=new StringBuilder();
            }
            else{
                ans+=str;
                cnt++;
            }
        }
        if(cnt%2!=0){
            sb.append(s.substring(i,s.length()));
            sb.reverse();
            ans+=sb.toString();
        }
        else{
            ans+=s.substring(i,s.length());
        }
        return ans;
    }
}
Time: O(n)  Space: O(1)

class Solution {
public:
    string reverseStr(string s, int k) {
        int n=size(s);
        for(int i=0;i<n;i=i+2*k){
            int j=i+k-1,k=i;
            if(j>=n) 
                j=n-1;
            while(k<(j)){
                swap(s[k],s[j]);
                k++,j--;
            }
        }
        return s;
    }
};
/**
 * @param {string} s
 * @param {number} k
 * @return {string}
 */
var reverseStr = function(s, k) {
    const sArr = s.split('');

    let start = 0;
    let end = k - 1;

    const swapBlock = (start, end) => {
        while (start < end) {
            [sArr[start], sArr[end]] = [sArr[end], sArr[start]];

            start++;
            end--;
        }
    };

    while (start < end) {
        swapBlock(start, end);

        start = start + (k * 2);
        end = sArr[start + (k-1)] ? start + (k-1) : s.length - 1;
    }

    return sArr.join('');
};
Reverse String II