华为OD-第K长的连续字母字符串长度

题目描述

给定一个字符串,只包含大写字母,求在包含同一字母的子串中,长度第 k 长的子串的长度,相同字母只取最长的那个子串。

代码实现

python 复制代码
# coding:utf-8
# 第K长的连续字母字符串长度
# https://www.nowcoder.com/discuss/353150502185672704?sourceSSR=search

class Solution:
    def maxContinuousStr(self, s):
        res = dict()
        count = 1
        for i in range(len(s) - 1):
            if s[i] == s[i + 1]:
                count += 1
            else:
                if s[i] in res:
                    res[s[i]] = max(res[s[i]], count)
                else:
                    res[s[i]] = count
                count = 1
        ret = sorted(res.items(), key=(lambda x: x[1]), reverse=True)
        return ret[k-1][1]


if __name__ == '__main__':
    s = input("input S:").split(' ')[0]
    k = int(input('input K :').split(' ')[0])
    solution = Solution()
    print(solution.maxContinuousStr(s))
相关推荐
JieE21215 小时前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
金銀銅鐵20 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup111 天前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi001 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf1 天前
Agent 流程编排
后端·python·agent
copyer_xyf1 天前
Agent RAG
后端·python·agent
copyer_xyf1 天前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf1 天前
Agent 记忆管理
后端·python·agent