注意python的字符和数字转化函数是chr和ord
class Solution: def licenseKeyFormatting(self, S: str, K: int) -> str: lst = [] for c in S: if c == '-': continue if c >= 'a' and c <= 'z': c = chr(ord(c) - ord('a') + ord('A')) lst.append(c) ret = '' for i in range(len(lst)): ret += lst[i] rem = len(lst) % K - 1 if rem < 0: rem += K if i != len(lst) - 1 and i % K == rem: ret += '-' return ret