当前位置:网站首页>LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
2022-07-02 15:15:00 【I'm up and down in the Jianghu】
1. subject
Give you a string s And an integer k , Please find out s Longest substring in , The number of occurrences of each character in the substring shall not be less than k . Returns the length of this substring .
Example 1:
Input :s = “aaabb”, k = 3
Output :3
explain : The longest string is “aaa” , among ‘a’ repeated 3 Time .
Example 2:
Input :s = “ababbc”, k = 2
Output :5
explain : The longest string is “ababb” , among ‘a’ repeated 2 Time , ‘b’ repeated 3 Time .
Tips :
1 <= s.length <= 104
s It's only made up of lowercase letters
1 <= k <= 105
source : Power button (LeetCode)
link :https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters
2. Ideas
(1) The law of violent exhaustion
This method is easy to think of , Namely Traversal string s All lengths of are greater than or equal to k The string of , Judge the number of occurrences of each character in them , And use res Record the length of the longest substring that meets the conditions , After traversal, return res that will do , In the judgment process, you can use a hash table to store the number of occurrences of each string . But the time complexity of this method is high , stay LeetCode Submit on will appear “ Time limit exceeded ” A hint of .
(2) The sliding window
Train of thought reference Official solution to this problem .
3. Code implementation (Java)
// Ideas 1———— The law of violent exhaustion
class Solution {
public int longestSubstring(String s, int k) {
// key / value : character / Corresponding number of occurrences
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++) {
// Get substring
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;
}
}
// Ideas 2———— The sliding window
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;
}
}
边栏推荐
猜你喜欢
LeetCode 2310. 个位数字为 K 的整数之和
Table responsive layout tips
LeetCode 209. Minimum length subarray
06_栈和队列转换
Jenkins Pipeline 应用与实践
Mavn 搭建 Nexus 私服
Why can't programmers who can only program become excellent developers?
[untitled] leetcode 2321 Maximum score of concatenated array
Practical debugging skills
[c voice] explain the advanced pointer and points for attention (2)
随机推荐
【C语言】详解指针的初阶和进阶以及注意点(1)
LeetCode 2310. The number of digits is the sum of integers of K
【题解】Educational Codeforces Round 82
牛客练习赛101
871. 最低加油次数 : 简单优先队列(堆)贪心题
LeetCode 209. Minimum length subarray
kibana 基础操作
05_队列
使用 TiUP 部署 TiDB 集群
TiDB 环境与系统配置检查
C语言实现N皇后问题
[C language] explain the initial and advanced levels of the pointer and points for attention (1)
php获取数组中键值最大数组项的索引值的方法
Tidb data migration scenario overview
MFC console printing, pop-up dialog box
【C语音】详解指针进阶和注意点(2)
记一次报错解决经历依赖重复
. Net core logging system
电脑怎么设置扬声器播放麦克风的声音
MFC 控制台打印,弹出对话框