JavaScript solution (not faster .just an idea)


  • 0
    S
    var reverseStr = function(s, k) {
        if(s.length <= k ) return s.split("").reverse().join("");
        var res = [];
        s = s.split("");
        while(s.length>k){
            res.push(...(s.splice(0,k).reverse()));
            res = res.concat(s.splice(0,k));
        }
        res.push(...(s.reverse()));
        return res.join("");
    };
    

Log in to reply
 

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