Javascript solution


  • 0
    R
    var reverseStr = function(s, k) {
        var res = "";
        
        for(var i = 0; i < s.length; i+=(2*k)){
            res += reverse(s.substr(i, k));
            res += s.substr(i+k, k);
        }
        
        return res;
    };
    
    
    var reverse = function(s){
        var chars = s.split(''),
            first = 0,
            last = s.length - 1,
            temp;
        
        while(first < last){
            temp = chars[first];
            chars[first] = chars[last];
            chars[last] = temp;
            first++;
            last--;
        }
        
        return chars.join('');
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.