?LeetCode刷題實(shí)戰(zhàn)395:至少有 K 個(gè)重復(fù)字符的最長子串
Given a string s and an integer k, return the length of the longest substring of s such that the frequency of each character in this substring is greater than or equal to k.
示例
示例 1:
輸入:s = "aaabb", k = 3
輸出:3
解釋:最長子串為 "aaa" ,其中 'a' 重復(fù)了 3 次。
示例 2:
輸入:s = "ababbc", k = 2
輸出:5
解釋:最長子串為 "ababb" ,其中 'a' 重復(fù)了 2 次, 'b' 重復(fù)了 3 次。
解題
class Solution {
public int longestSubstring(String s, int k) {
if(k<=1)
return s.length();
char[] arr = s.toCharArray();
int size = 0;
for(int i=0;i<arr.length;i++){
int[]mip =new int[26];
mip[arr[i]-'a']++;
for(int j=i+1;j<arr.length;j++){
mip[arr[j]-'a']++;
if(arrBiggerK(mip,k)==true && j-i+1>size){
size = j-i+1;
}
}
}
return size;
}
private boolean arrBiggerK(int[] arr,int k){
for(int c : arr){
if(c > 0 && c < k){
return false;
}
}
return true;
}
};
LeetCode1-380題匯總,希望對你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)381:O(1) 時(shí)間插入、刪除和獲取隨機(jī)元素
LeetCode刷題實(shí)戰(zhàn)382:鏈表隨機(jī)節(jié)點(diǎn)
LeetCode刷題實(shí)戰(zhàn)383:贖金信
LeetCode刷題實(shí)戰(zhàn)384:打亂數(shù)組
LeetCode刷題實(shí)戰(zhàn)385:迷你語法分析器
