华为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))
相关推荐
不会就选b2 小时前
算法日常・每日刷题--<贪心>25
数据结构·算法
杨杨杨大侠2 小时前
Jev、Kev、Laya:决策模型怎么选,什么时候需要微调?
人工智能·python·agent
LOVE️YOU2 小时前
Python 在定义函数时,究竟定义的是什么?
python
段一凡-华北理工大学2 小时前
大模型应用开发 100 天:Python + LLM 从入门到精通 day26~Prompt 调试与优化——A/B 测试与效果评估
windows·python·大模型·prompt·智能体·提示词工程·高炉智能化
Minecraft红客3 小时前
以撒的结合
python·游戏·电脑·娱乐
Circ.3 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python
风早爽太3 小时前
用 ‌Render‌ 快速部署网站和常见问题解决
python·fastapi
李航19835 小时前
给自己的图形引擎,配上了AI渲染,做设计真是太方便了
人工智能·python·计算机视觉·ai·ai编程
L@ncor5 小时前
第六章 框架开发实践 · 学习笔记(AutoGen / AgentScope / CAMEL / LangGraph)
python·框架·autogen·langgraph·agentscope
Zhou1411365 小时前
SpringSecurity_02_授权与高级功能
开发语言·python