3 lines in Python (replace - join - list comprehension)

  • 0
    class Solution(object):
        def licenseKeyFormatting(self, S, K):
            S = S.replace('-','').upper()
            N = len(S)
            return S[:N%K] + ('-' if N%K and N>K else '') + '-'.join(S[i:i+K] for i in xrange(N%K,N,K))

Log in to reply

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