Straightforward and easy-to-understand Python solution


  • 0
    class Solution(object):
        def licenseKeyFormatting(self, S, K):
            r = ''
            n = len(S)
            count = 0
            for i in range(n-1, -1, -1):
                if S[i] != '-':
                    r = S[i].upper() + r
                    count += 1
                if count == K:
                    r = '-' + r
                    count = 0
            if len(r) == 0 or r[0] != '-': return r
            return r[1:len(r)]
    

Log in to reply
 

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