当前位置:网站首页>Xiaohei's leetcode journey: 95. Longest substring with at least K repeating characters
Xiaohei's leetcode journey: 95. Longest substring with at least K repeating characters
2022-08-04 23:20:00 【little black invincible】
小黑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 = {
}
# Record the frequency of each character in the substring
for c in s[start:end+1]:
counts[c] = counts.get(c,0) + 1
# Filter out frequencies less thank的一个字符
split = None
for c in counts.keys():
if counts[c] < k:
split = c
break
# All characters meet the requirements,则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)
边栏推荐
- The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?
- Since a new byte of 20K came out, I have seen what the ceiling is
- [QNX Hypervisor 2.2用户手册]10.6 vdev mc146818
- uniapp横向选项卡(水平滚动导航栏)效果demo(整理)
- 当panic或者die被执行时,或者发生未定义指令时,如何被回调到
- 基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
- 【七夕快乐篇】Nacos是如何实现服务注册功能的?
- 4-《PyTorch深度学习实践》-反向传播
- typeScript-promise
- 加解密在线工具和进制转化在线工具
猜你喜欢
随机推荐
【字符串函数内功修炼】strlen + strstr + strtok + strerror(三)
4-《PyTorch深度学习实践》-反向传播
Service Mesh落地路径
【云原生 · Kubernetes】Kubernetes运维
The role of @Async annotation and how to implement asynchronous listening mechanism
Nuclei (2) Advanced - In-depth understanding of workflows, Matchers and Extractors
年薪50W+的测试工程师都在用这个:Jmeter 脚本开发之——扩展函数
MySQL的安装与卸载
【游戏建模模型制作全流程】使用ZBrush制作骷髅王
基于Appian低代码平台开发一个SpaceX网站
被领导拒绝涨薪申请,跳槽后怒涨9.5K,这是我的心路历程
The Go Programming Language (Introduction)
Pytorch分布式训练/多卡/多GPU训练DDP的torch.distributed.launch和torchrun
MySQL基础篇【聚合函数】
【SSR服务端渲染+CSR客户端渲染+post请求+get请求+总结】
社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
407. 接雨水 II
Uniapp dynamic sliding navigation effect demo (finishing)
话题 | 雾计算和边缘计算有什么区别?
【手撕AHB-APB Bridge】~ AMBA总线 之 AHB