Python Basic String Operation


  • 0
    class Solution(object):
        def licenseKeyFormatting(self, S, K):
            s = S.replace('-','').upper()
            if len(s) < K:
                return s
            wl, fl = len(s) / K, len(s) % K
            ans = ''
            for i in range(fl,len(s),K):
                ans += s[i:i+K] + '-'
            return ans[:-1] if fl == 0 else s[:fl] +'-' + ans[:-1]

Log in to reply
 

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