6-line python solution


  • 0
    M
    class Solution(object):
        def licenseKeyFormatting(self, S, K):
            """
            :type S: str
            :type K: int
            :rtype: str
            """
            S = ''.join(S.split('-')).upper()   
            i = len(S) - K  # index to insert '-', from back to front
            while i > 0:
                S = S[:i] + '-' + S[i:] 
                i -= K
            return S
    

Log in to reply
 

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