当前位置:网站首页>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
- obsidian安装第三方插件——无法加载插件
- fatal: unsafe repository is owned by someone else 的解决方法
- MathML to latex
- <口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
- Advanced usage of C language -- function pointer: callback function; Conversion table
- 复用和分用
- 卷积神经网络(入门)
- Fabric.js 缩放画布
- Pychart connects to the remote server
猜你喜欢

使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式

Teamtalk source code analysis win client

What is erdma? Popular science cartoon illustration

Factal: Unsafe repository is owned by someone else Solution

STM32库函数进行GPIO初始化

Xilinx Vivado set *. svh as SystemVerilog Header

TeamTalk源码分析之win-client

STM32 library function for GPIO initialization

obsidian安装第三方插件——无法加载插件

【apipost】使用教程
随机推荐
Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
fatal: unsafe repository is owned by someone else 的解决方法
数据库连接池和数据源
How many knowledge points can a callable interface have?
Delete element (with transition animation)
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
Contrôleur pour threejs cube Space Basic Controller + Inertial Control + Flight Control
Fabric.js 自由绘制椭圆
1. Editing weapon VIM
Database connection pool and data source
没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
Fabric. JS free drawing ellipse
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
篇9:XShell免费版安装
Simple verification code generator for 51 single chip microcomputer experiment
Use of freemaker
fatal: unsafe repository is owned by someone else 的解决方法
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
Pychart connects to the remote server
[QNX hypervisor 2.2 user manual]6.3 communication between guest and external