【leetcode】395. Longest Substring with At Least K Repeating Characters
题目如下:
解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于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的更多相关文章
- 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 395. Longest Substring with At Least K Repeating Characters
395. Longest Substring with At Least K Repeating Characters 我的思路是先扫描一遍,然后判断是否都满足,否则,不满足的字符一定不出现,可以作为 ...
- 【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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- leetcode 395. Longest Substring with At Least K Repeating Characters(高质量题)
只能说还是太菜,抄的网上大神的做法: idea: mask 的每一位代表该位字母够不够k次,够k次为0,不够为1 对于每一位将其视为起点,遍历至末尾,找到其最大满足子串T的下标max_idx,之后从m ...
- 395 Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子串
找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k .输出 T 的长度.示例 1:输入:s = "aaabb", k = 3输出:3最 ...
随机推荐
- Java double转long方法
Java double转long方法 double random = Math.round(Math.random()*10000); long l = new Double(random).long ...
- Java Web学习总结(10)学习总结-EL表达式
一,EL 表达式概述(EL主要从域中取数据) EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL出现的目的是要替代jsp页面中脚本的编写. 二,EL从域中 ...
- 谷歌已经对Android的开源严防死守
上周,沸沸扬扬的Android垄断案把Google又一次推向了风口浪尖,在这次的垄断案中,Google被欧盟起诉赔偿50亿美元,被起诉的其中一个原因是Google对外宣称Android是开放的,但其实 ...
- PHP之GET和POST小结
PHP之GET和POST小结 PHP $_GET 变量 $_GET 变量 预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值. 从带有 GET 方法的表 ...
- [bzoj3462]DZY Loves Math II (美妙数学+背包dp)
Description Input 第一行,两个正整数 S 和 q,q 表示询问数量. 接下来 q 行,每行一个正整数 n. Output 输出共 q 行,分别为每个询问的答案. Sample Inp ...
- GH001 on github
remote: warning: GH001: Large files detected. You may want to try Git Large File Storage - https://g ...
- [MAC]配置Jenkins 开机自启动
如果是将jenkins.war放在tomcat中运行的, 则可以配置开机启动tomcat,脚本如下: XXX表示是你安装Tomcat所在目录 #启动tomcat cd XXX/Tomcat8/bin ...
- leetcode-解题记录 771. 宝石与石头
题目: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符 ...
- drf基础
1.什么是编程? 数据结构和算法的结合 2.什么是REST? 同一个功能会产生五花八门的url(把查看单条记录和查看多条记录都看成是一个功能),而且响应回去的数据也没有同一的格式规范,这就造成了前后端 ...
- HBaseRegionServer宕机数据恢复
本文由 网易云 发布 作者:范欣欣 本篇文章仅限内部分享,如需转载,请联系网易获取授权. 众所周知,HBase默认适用于写多读少的应用,正是依赖于它相当出色的写入性能:一个100台RS的集群可以轻松地 ...