solution by JS


  • 0
    T
    /**
     * @param {string} s
     * @param {number} k
     * @return {string}
     */
    var reverseStr = function(s, k) {
        var start=0;
        var end=2*k;
        var arr=[];
        var result=""
        if(s.length>(2*k)){
        while(end<=s.length){
            
            var st=s.slice(start,end);
            arr.push(st);
            start=end;
            end=end+2*k;
        }
        st=s.slice(start,s.length);
        arr.push(st);
        for(var i=0;i<arr.length;i++){
            
            result=result+rever(arr[i],k);
        }
    
        return result;
        }
       else{
           return rever(s,k);
       }
    };
    
    function rever(item, num){
        
        var arr1=item.slice(0,num).split("");
        var st=item.slice(num,item.length);
        var result=arr1[arr1.length-1];
        for(var i=(arr1.length-2);i>=0;i--){
            
            result=result+arr1[i];
        }
        return result+st;
        
    }
    

Log in to reply
 

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