当前位置:网站首页>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)

边栏推荐
- PID控制器改进笔记之七:改进PID控制器之防超调设定
- 使用代理对象执行实现类目标方法异常
- npm基本操作及命令详解
- 365天深度学习训练营-学习线路
- 当panic或者die被执行时,或者发生未定义指令时,如何被回调到
- 堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观
- 为何越来越多人选择进入软件测试行业?深度剖析软件测试的优势...
- [Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)
- 应用联合、体系化推进。集团型化工企业数字化转型路径
- The Controller layer code is written like this, concise and elegant!
猜你喜欢
随机推荐
堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观
4-《PyTorch深度学习实践》-反向传播
【3D建模制作技巧分享】ZBrush纹理贴图怎么导入
360市值四年蒸发3900亿,政企安全能救命吗?
go语言的time包介绍
web3.js
请你说一下final关键字以及static关键字
MySQL增删改查基础
TypeScript - the use of closure functions
逆序对的数量
容联云发送短信验证码
吐槽 | 参加IT培训的正确姿势
2022年华数杯数学建模
2022/8/3
kernel hung_task死锁检测机制原理实现
特征工程资料汇总
当panic或者die被执行时,或者发生未定义指令时,如何被回调到
Laravel 实现redis分布式锁
PID控制器改进笔记之七:改进PID控制器之防超调设定
应用联合、体系化推进。集团型化工企业数字化转型路径









