当前位置:网站首页>小黑leetcode之旅:95. 至少有 K 个重复字符的最长子串
小黑leetcode之旅:95. 至少有 K 个重复字符的最长子串
2022-08-04 23:13:00 【小黑无敌】
小黑java解法1:分治法
class Solution {
public int longestSubstring(String s, int k) {
return dfs(s, 0, s.length() - 1, k);
}
public int dfs(String s, int left, int right, int k) {
if (left > right) {
return 0;
}
int[] cnt = new int[26];
for (int i = left; i <= right; i++) {
cnt[s.charAt(i) - 'a']++;
}
int res = 0;
int start = left;
boolean flag = false;
System.out.println(left+"+"+right);
for(int i = left; i <= right; i++) {
if (cnt[s.charAt(i) - 'a'] < k) {
System.out.println(i);
int p = dfs(s, start, i-1, k);
if(p > res){
res = p;
}
flag = true;
start = i + 1;
}
}
if(flag){
int p = dfs(s, start, right, k);
if(p > res){
res = p;
}
return res;
}
return right - left + 1;
}
}

小黑python解法1:分治法
class Solution:
def longestSubstring(self, s: str, k: int) -> int:
l = len(s)
def substring(s,start,end):
counts = {
}
for c in s[start:end+1]:
counts[c] = counts.get(c,0) + 1
# 生成分割点
splits = []
for key in counts:
if counts[key] < k:
splits.append(key)
if not splits:
return end - start + 1
i = start
res = 0
while i <= end:
while i <= end and s[i] in splits:
i += 1
if i > end:
break
start = i
while i <= end and s[i] not in splits:
i += 1
length = substring(s,start,i-1)
res = max(length,res)
return res
return substring(s,0,l-1)

分治法
class Solution:
def longestSubstring(self, s: str, k: int) -> int:
l = len(s)
def subString(start,end):
counts = {
}
# 记录子串中每一个字符的频率
for c in s[start:end+1]:
counts[c] = counts.get(c,0) + 1
# 筛选出频率小于k的一个字符
split = None
for c in counts.keys():
if counts[c] < k:
split = c
break
# 所有字符符合要求,则return
if not split:
return end - start + 1
i = start
ans = 0
while start <= end:
while i <= end and s[i] == split:
i += 1
if i > end:
break
start = i
while i <= end and s[i] != split:
i += 1
ans = max(ans,subString(start,i-1))
return ans
return subString(0,l-1)

边栏推荐
- 应用联合、体系化推进。集团型化工企业数字化转型路径
- 正则表达式绕过
- temp7777
- The Controller layer code is written like this, concise and elegant!
- 【SSR服务端渲染+CSR客户端渲染+post请求+get请求+总结】
- Kernel函数解析之kernel_restart
- MySQL基础篇【子查询】
- [Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)
- 基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
- 【3D建模制作技巧分享】ZBrush如何重新拓扑
猜你喜欢
![[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)](/img/96/946bbef52bd017ac6142c6b7485a86.png)
[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)

Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer

App测试和Web测试的区别
![[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)](/img/b6/5a1c8b675dc7f67f359c25908403e1.png)
[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)

2022年全网最全接口自动化测试框架搭建,没有之一

golang打开文件和读写文件

365天深度学习训练营-学习线路

kernel hung_task死锁检测机制原理实现

轮播图动态渲染

Kernel函数解析之kernel_restart
随机推荐
npm基本操作及命令详解
Nacos配置中心之客户端长轮询
truffle
【游戏建模模型制作全流程】在ZBrush中雕刻恶魔城男性角色模型
SRv6网络的安全解决方案
基于深度学习的路面坑洞检测(详细教程)
The Record of Reminding myself
仪表板展示 | DataEase看中国:数据呈现中国资本市场
407. 接雨水 II
一点点读懂regulator(四)
堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观
文献阅读十——Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
被领导拒绝涨薪申请,跳槽后怒涨9.5K,这是我的心路历程
Linear DP (bottom)
亿流量大考(3):不加机器,如何抗住每天百亿级高并发流量?
[QNX Hypervisor 2.2用户手册]10.4 vdev hpet
Go 编程语言(简介)
Service Mesh落地路径
postman接口测试
Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn