Short Python


  • 0

    Just keep in mind if S can be split into K parts evenly, we need to strip "-" on the left.

    class Solution(object):
        def licenseKeyFormatting(self, S, K):
            """
            :type S: str
            :type K: int
            :rtype: str
            """
            S = "".join(S.upper().split("-"))
            end = len(S) % K
            ans = "-".join([S[:end]] + [S[i:i+K] for i in range(end, len(S), K)])
            return ans.lstrip("-")
    

Log in to reply
 

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