当前位置:网站首页>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;
}
}
边栏推荐
- taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
- Delete element (with transition animation)
- Development and design of animation surrounding mall sales website based on php+mysql
- Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
- threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
- Reuse and distribution
- 数据库连接池和数据源
- Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
- Basic knowledge of QT original code
- taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
猜你喜欢
Pychart connects to the remote server
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
The most complete analysis of Flink frame window function
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
Daily learning 2
快解析:轻松实现共享上网
Fabric.js 自由绘制圆形
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
Wechat applet uses towxml to display formula
跨服务器数据访问的创建链接服务器方法
随机推荐
Find the maximum inscribed circle of the contour
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
How kaggle uses utility script
天猫商品详情接口(APP,H5端)
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
STM32 standard firmware library function name memory (II)
Implement a server with multi process concurrency
Daily learning 3
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
OpenCV调用USB摄像头的点滴
taobao. trade. Get (get some information of a single transaction), Taobao store order interface, Taobao oauth2.0 interface, Taobao R2 interface code docking and sharing
uniapp自动化测试学习
Solving the longest subsequence with linear DP -- three questions
Large top heap, small top heap and heap sequencing
由粒子加速器产生的反中子形成的白洞
[QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
Xilinx Vivado set *.svh as SystemVerilog Header
使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
删除元素(带过渡动画)
复用和分用