482. 密钥格式化

链接

https://leetcode.cn/problems/license-key-formatting/description/

思路

字符串处理,没啥好说的...

代码

class Solution:
    def licenseKeyFormatting(self, s: str, k: int) -> str:
        valid_len, valid_s = self.get_length(s)
        first_len = valid_len % k if valid_len % k != 0 else k
        res = valid_s[:first_len]
        for i in range(first_len, valid_len, k):
            res += '-' + valid_s[i: i+k]
        return res

    def get_length(self, s):
        res = 0
        res_s = ''
        for i in s:
            if i != '-':
                res += 1
                res_s += i.upper()
        return res, res_s

 

posted @ 2023-09-06 09:43  BJFU-VTH  阅读(6)  评论(0编辑  收藏  举报