当前位置:网站首页>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;
}
}
边栏推荐
- Advanced usage of C language -- function pointer: callback function; Conversion table
- Fabric. Usage of JS eraser (including recovery function)
- ##51单片机实验之简易验证码发生器
- Fabric. JS free drawing ellipse
- Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
- A white hole formed by antineutrons produced by particle accelerators
- 测试框架TestNG的使用(二):testNG xml的使用
- taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
- PHP linked list creation and traversal
- C#代码审计实战+前置知识
猜你喜欢
Makefile 分隔文件名与后缀
没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
Daily learning 3
buuctf-pwn write-ups (7)
< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
fatal: unsafe repository is owned by someone else 的解决方法
PHP linked list creation and traversal
Socket and socket address
随机推荐
Fabric. Usage of JS eraser (including recovery function)
NLA自然语言分析,让数据分析更智能
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
C语言高级用法--函数指针:回调函数;转换表
3、函数指针和指针函数
buuctf-pwn write-ups (7)
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
Delete element (with transition animation)
[development environment] StarUML tool (download software | StarUML installation | StarUML creation project)
没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
Fabric.js 自由绘制圆形
Fundamentals of software testing
Fabric. JS zoom canvas
Contrôleur pour threejs cube Space Basic Controller + Inertial Control + Flight Control
tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
Stm32-dac Experiment & high frequency DAC output test
关于Flink框架窗口(window)函数最全解析
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署