当前位置:网站首页>小黑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)
边栏推荐
- 社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
- MySQL基础篇【聚合函数】
- 【内存操作函数内功修炼】memcpy + memmove + memcmp + memset(四)
- The Go Programming Language (Introduction)
- typeScript-闭包函数的使用
- Linux系统重启和停止Mysql服务教程
- PHP(3)
- [Mock Interview - 10 Years of Work] Are more projects an advantage?
- C5750X7R2E105K230KA(电容器)MSP430F5249IRGCR微控制器资料
- 测试技术:关于上下文驱动测试的总结
猜你喜欢
养殖虚拟仿真软件提供高沉浸式的虚拟场景互动操作体验学习
直接插入排序
一点点读懂cpufreq(一)
堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观
I was rejected by the leader for a salary increase, and my anger rose by 9.5K after switching jobs. This is my mental journey
go语言的日志实现(打印日志、日志写入文件、日志切割)
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
PID Controller Improvement Notes No. 7: Improve the anti-overshoot setting of the PID controller
为何越来越多人选择进入软件测试行业?深度剖析软件测试的优势...
随机推荐
ClickHouse 二级索引
App测试和Web测试的区别
Acwing3593. 统计单词
MySQL的安装与卸载
轮播图动态渲染
生产者消费者问题
话题 | 雾计算和边缘计算有什么区别?
特征工程资料汇总
期货开户哪个平台好,要正规安全的
一点点读懂cpufreq(二)
为何越来越多人选择进入软件测试行业?深度剖析软件测试的优势...
【项目实战】仿照Room实现简单管理系统
社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
Web安全开发 | 青训营笔记
【软件测试】常用ADB命令
【游戏建模模型制作全流程】在ZBrush中雕刻恶魔城男性角色模型
Redisson
postman接口测试
【字符串函数内功修炼】strcpy + strcat + strcmp(一)
Pytest学习-Fixture