- 密钥格式化:真的不难
···
class Solution:
def licenseKeyFormatting(self, S: str, K: int) -> str:
res = ''
count = 0
for i in S[::-1]:
if i == '-':
continue
if i.isalnum():
res += i.upper()
count += 1
if count == K:
res += '-'
count = 0
if len(res) != 0 and res[-1] == '-': # 这里是因为在末尾添加了不必要的‘-’,所以要手动删掉
res = res[:-1]
res = res[::-1]
return res
···