js solution with 1 traverse


  • 0
    C
    var licenseKeyFormatting = function(S, K) {
        var result = [];
        for (var i = S.length - 1; i >= 0; i--) {
            var c = S.charAt(i);
            if (c !== '-') result.unshift(c.toUpperCase());
            if (result[K] === '-' || result.length === K) result.unshift('-');
        }
        if (result[0] === '-') result.shift();
        return result.join('');
    };
    
    

Log in to reply
 

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