题目如下:

解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于k,因此出现次数小于k的字符就一定不能出现,所以就可以以这些字符作为分隔符分割成多个子串,然后继续对子串递归,找出符合条件的子串。

代码如下:

class Solution(object):
res = 0
def splitToSubs(self,subs,k):
dic = {}
for i in subs:
if i not in dic:
dic[i] = 1
else:
dic[i] += 1
exclusive = []
for i, v in enumerate(subs):
if dic[v] < k:
exclusive.append(i)
if len(exclusive) == 0:
self.res = max(self.res,len(subs))
return
start, end = 0, len(subs) - 1
for i in (exclusive):
if i - start < k:
start = i
continue
else:
tmp = subs[start:i]
self.splitToSubs(tmp,k)
start = i
if end - exclusive[-1] >= k:
self.splitToSubs(subs[exclusive[-1] + 1:end + 1],k)
def longestSubstring(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
self.res = 0
self.splitToSubs(s,k)
return self.res

【leetcode】395. Longest Substring with At Least K Repeating Characters的更多相关文章

  1. 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  2. 395. Longest Substring with At Least K Repeating Characters

    395. Longest Substring with At Least K Repeating Characters 我的思路是先扫描一遍,然后判断是否都满足,否则,不满足的字符一定不出现,可以作为 ...

  3. 【LeetCode】159. Longest Substring with At Most Two Distinct Characters

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...

  4. [LeetCode] 395. Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. 2016/9/21 leetcode 解题笔记 395.Longest Substring with At Least K Repeating Characters

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  6. LeetCode 395. Longest Substring with At Least K Repeating Characters C#

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  7. leetcode 395. Longest Substring with At Least K Repeating Characters

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  8. leetcode 395. Longest Substring with At Least K Repeating Characters(高质量题)

    只能说还是太菜,抄的网上大神的做法: idea: mask 的每一位代表该位字母够不够k次,够k次为0,不够为1 对于每一位将其视为起点,遍历至末尾,找到其最大满足子串T的下标max_idx,之后从m ...

  9. 395 Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子串

    找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k .输出 T 的长度.示例 1:输入:s = "aaabb", k = 3输出:3最 ...

随机推荐

  1. 英语单词escapes

    escapes 来源 [root@centos7 ~]# help echo echo: echo [-neE] [arg ...] Write arguments to the standard o ...

  2. Linux进程管理——查看内存的工具

    Linux进程管理——查看内存的工具 一查看内存的工具vmstat vmstat命令:虚拟内存信息vmstat [options] [delay [count]]vmstat 2 5 [root@ce ...

  3. spring-boot整合mybaits多数据源动态切换案例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...

  4. php strnatcmp()函数 语法

    php strnatcmp()函数 语法 作用:自然顺序法比较字符串直线往复电机 语法:strnatcmp(string1,string2) 参数: 参数 描述 string1 必须,规定要比较的第一 ...

  5. 2019牛客多校第六场 B - Shorten IPv6 Address 模拟

    B - Shorten IPv6 Address 题意 给你\(128\)位的二进制,转换为十六进制. 每\(4\)位十六进制分为\(1\)组,每两组用一个\(":"\)分开. 每 ...

  6. 【HDOJ6598】Harmonious Army(最小割)

    题意:有n个人,每个人可以从A,B两种职业中选择一种 有m对两人组,如果两个人都是A能获得p的收益,一个A一个B能获得q的收益,都是B能获得r的收益,其中q=p/4+r/3,保证p%4=0,r%3=0 ...

  7. tracert命令 -网络管理命令

    Tracert是路由跟踪程序,用于确定 IP 数据报访问目标所经过的路径.Tracert 命令用 IP 生存时间 (TTL) 字段和 ICMP 错误消息来确定从一个主机到网络上其他主机的路由. 在工作 ...

  8. Codeforces Round #578 (Div. 2) E. Compress Words (双哈希)

    题目:https://codeforc.es/contest/1200/problem/E 题意:给你n个单词,你需要把他合成成一个句子,相邻的两个单词,相邻部分相同的话可以把其中一个的删掉 思路:因 ...

  9. zkw 线段树

    优秀的 zkw 线段树讲解:<线段树的扩展之浅谈 zkw 线段树> 存一份模板代码(区间修改.区间查询): /* zkw Segment Tree * Au: GG */ #include ...

  10. char指针类型的传值和传址