当前位置:网站首页>LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串
LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串
2022-07-02 11:24:00 【一瓢江湖我沉浮】
1.题目
给你一个字符串 s 和一个整数 k ,请你找出 s 中的最长子串, 要求该子串中的每一字符出现次数都不少于 k 。返回这一子串的长度。
示例 1:
输入:s = “aaabb”, k = 3
输出:3
解释:最长子串为 “aaa” ,其中 ‘a’ 重复了 3 次。
示例 2:
输入:s = “ababbc”, k = 2
输出:5
解释:最长子串为 “ababb” ,其中 ‘a’ 重复了 2 次, ‘b’ 重复了 3 次。
提示:
1 <= s.length <= 104
s 仅由小写英文字母组成
1 <= k <= 105
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters
2.思路
(1)暴力穷举法
该方法比较容易想到,就是遍历字符串 s 的所有长度大于等于 k 的子串,对它们中的每一字符出现的次数进行判断,并用 res 记录符合条件的最长子串的长度,遍历结束后返回 res 即可,在判断过程可以使用哈希表来存储每个字符串出现的次数。只不过该方法的时间复杂度较高,在 LeetCode 上提交会出现“超出时间限制”的提示。
(2)滑动窗口
思路参考本题官方题解。
3.代码实现(Java)
//思路1————暴力穷举法
class Solution {
public int longestSubstring(String s, int k) {
//键/值:字符/对应出现的次数
HashMap<Character, Integer> hashMap = new HashMap<>();
int length = s.length();
int res = 0;
for (int i = 0; i < length; i++) {
for (int j = i + k; j <= length; j++) {
//获取子串
String subStr = s.substring(i, j);
boolean flag = true;
hashMap.clear();
for (int l = 0; l < subStr.length(); l++) {
char c = subStr.charAt(l);
hashMap.put(c, hashMap.getOrDefault(c, 0) + 1);
}
for (int cnt : hashMap.values()) {
if (cnt < k) {
flag = false;
break;
}
}
if (flag == true) {
res = Math.max(res, subStr.length());
}
}
}
return res;
}
}
//思路2————滑动窗口
class Solution {
public static int longestSubstring(String s, int k) {
int res = 0;
int length = s.length();
for (int i = 1; i <= 26; i++) {
int left = 0;
int right = 0;
int[] cnt = new int[26];
int total = 0;
int less = 0;
while (right < length) {
cnt[s.charAt(right) - 'a']++;
if (cnt[s.charAt(right) - 'a'] == 1) {
total++;
less++;
}
if (cnt[s.charAt(right) - 'a'] == k) {
less--;
}
while (total > i) {
cnt[s.charAt(left) - 'a']--;
if (cnt[s.charAt(left) - 'a'] == k - 1) {
less++;
}
if (cnt[s.charAt(left) - 'a'] == 0) {
total--;
less--;
}
left++;
}
if (less == 0) {
res = Math.max(res , right - left + 1);
}
right++;
}
}
return res;
}
}
边栏推荐
- Development and design of animation surrounding mall sales website based on php+mysql
- QT new project
- Solving the longest subsequence with linear DP -- three questions
- C语言高级用法--函数指针:回调函数;转换表
- Daily learning 2
- Fabric. Usage of JS eraser (including recovery function)
- Actual combat sharing of shutter screen acquisition
- NLA自然语言分析实现数据分析零门槛
- How kaggle uses utility script
- Makefile separates file names and suffixes
猜你喜欢

博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”

Fabric.js 自由绘制圆形

Makefile 分隔文件名与后缀

Pycharm连接远程服务器
[email protected]: The platform “win32“ is incompatible with this module."/>info [email protected]: The platform “win32“ is incompatible with this module.

Socket and socket address

##51单片机实验之简易验证码发生器

jmeter脚本参数化

Tmall product details interface (APP, H5 end)

PyQt5_ Qscrollarea content is saved as a picture
随机推荐
Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
[apipost] tutorial
Understanding of mongodb
Database connection pool and data source
Threejs controller cube space basic controller + inertia control + flight control
Fabric. Usage of JS eraser (including recovery function)
Pychart connects to the remote server
4. Array pointer and pointer array
How many knowledge points can a callable interface have?
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
快解析:轻松实现共享上网
[QNX hypervisor 2.2 user manual]6.3 communication between guest and external
天猫商品详情接口(APP,H5端)
篇9:XShell免费版安装
Use of freemaker
Fabric. JS free draw circle
socket(套接字)与socket地址
buuctf-pwn write-ups (7)
跨服务器数据访问的创建链接服务器方法