Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 13, 2018 07:19
Show Gist options
  • Save luojiyin1987/573e970991de2452d918eb5f6bfb0823 to your computer and use it in GitHub Desktop.
Save luojiyin1987/573e970991de2452d918eb5f6bfb0823 to your computer and use it in GitHub Desktop.
License Key Formatting
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