License Key Formatting

LeetCode 482

class Solution:
    def licenseKeyFormatting(self, S, K):
        """
        :type S: str
        :type K: int
        :rtype: str
        """
        """
        Input: S = "2-5g-3-J", K = 2
        Output: "2-5G-3J"
        
        一些指令:
        str.upper()
        str.lower()
        str.isalnum()
        str.replace()
        str.join([iterable])
        """
        S = S.replace("-", "").upper()[::-1]
        return '-'.join(S[i:i+K] for i in range(0, len(S), K))[::-1]

Last updated