Created
August 13, 2018 07:19
-
-
Save luojiyin1987/573e970991de2452d918eb5f6bfb0823 to your computer and use it in GitHub Desktop.
License Key Formatting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def licenseKeyFormatting(self, S, K): | |
| """ | |
| :type S: str | |
| :type K: int | |
| :rtype: str | |
| """ | |
| S =''.join(S.split('-')) | |
| S = str.upper(S) | |
| temp = '' | |
| len_S = len(S) | |
| first = len(S) % K | |
| count = 0 | |
| if len_S < K: | |
| return S | |
| if first != 0: | |
| temp = S[:first] + '-' | |
| for s in S[first:]: | |
| count += 1 | |
| temp += s | |
| if count % K == 0 and count < len_S - first: | |
| temp += '-' | |
| return temp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment