华为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))
相关推荐
用户497863050737 分钟前
(一)小红的数组操作
算法·编程语言
怕浪猫3 小时前
Electron 系列文章封面图
算法·架构·前端框架
黄忠4 小时前
01-系统架构设计-LangGraph状态机与多源异构RAG
python
zzzzzz3104 小时前
假如我是掘金管理员,我先给评论区装个'代码审查'系统
python·程序员·机器人
砍材农夫5 小时前
python环境|conda安装和使用(2)
后端·python
徐小夕5 小时前
JitWord 3.0 正式发布,高精度Word异构解析+复杂组件兼容,打造web端协同Word编辑器
前端·vue.js·算法
程序员龙叔17 小时前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
用户83562907805120 小时前
使用 Python 操作 Word 内容控件
后端·python
通信小呆呆20 小时前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?
人工智能·学习·算法·机器学习·机器人