华为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))
相关推荐
寻寻觅觅☆5 小时前
东华OJ-基础题-106-大整数相加(C++)
开发语言·c++·算法
YJlio5 小时前
1.7 通过 Sysinternals Live 在线运行工具:不下载也能用的“云端工具箱”
c语言·网络·python·数码相机·ios·django·iphone
偷吃的耗子6 小时前
【CNN算法理解】:三、AlexNet 训练模块(附代码)
深度学习·算法·cnn
l1t6 小时前
在wsl的python 3.14.3容器中使用databend包
开发语言·数据库·python·databend
化学在逃硬闯CS6 小时前
Leetcode1382. 将二叉搜索树变平衡
数据结构·算法
ceclar1237 小时前
C++使用format
开发语言·c++·算法
山塘小鱼儿7 小时前
本地Ollama+Agent+LangGraph+LangSmith运行
python·langchain·ollama·langgraph·langsimth
码说AI7 小时前
python快速绘制走势图对比曲线
开发语言·python
Gofarlic_OMS7 小时前
科学计算领域MATLAB许可证管理工具对比推荐
运维·开发语言·算法·matlab·自动化