【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最 ...
随机推荐
- js中的回钓函数,C#中的委托
$(function(){ myfunction(sayHi); }); var sayHi=function(){ alter('你好'); } function myfunction(a){ a( ...
- MySQL执行计划示例
以上示例来自尚硅谷!
- linux0.11源码内核——系统调用,int80的实现细节
linux0.11添加系统调用的步骤 假设添加一个系统调用foo() 1.修改include/linux/sys.h 添加声明 extern int foo(); 同时在sys_call_table数 ...
- MySQL-5.7填坑
绿色版(zip archive 版)无 my-default.ini As of MySQL 5.7.18, my-default.ini is no longer included in or in ...
- Process Monitor监控进程操作注册表如何实现?
http://zhidao.baidu.com/link?url=Kqav4qkQSprC5FnpHPOGJvhqvY9fJ9-Vdx9g_SWh4w5VOusdRJo4Vl7qIdrG4LwRJvr ...
- spring boot 尚桂谷学习笔记07 嵌入式容器 ---Web
------配置嵌入式servlet容器------ springboot 默认使用的是嵌入的Servlet(tomcat)容器 问题? 1)如何定制修改Servlet容器的相关配置: 1.修改和se ...
- 记录之前工作用到费劲sql
表为单独表,树结构 layer共有4层, 此sql为通过id list 查询出 layer = 2 的 id 个数 id , parent_id, layer SELECT COUNT(DISTINC ...
- B. pSort
题目链接: http://codeforces.com/problemset/problem/28/B 题意: 给一个n,原本有个1到n按顺序排列的序列,给一个序列问,在给一个数组,表示这个位置的数可 ...
- 利用print函数模拟打印进度条
import time , , ): time.sleep(0.1) num = i // 2 # 地板除,即取不大于/后的最小整数(3//2 = 1, 9//4 = 2, -7//2 = -4) s ...
- XGBoost的推导和说明
一.简介 XGBoost是“Extreme Gradient Boosting”的缩写,其中“Gradient Boosting”一词在论文Greedy Function Approximation: ...