My 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()
        val,res=divmod(len(s),K)
        ans,tmp="",s[res:]
        for i in range(val):
            ans+=tmp[i*K:(i+1)*K]+'-'*cmp(val-1,i)
        return s[:res]+'-'*cmp(res,0)*cmp(val,0)+ans

Log in to reply
 

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