题目:
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 less than or equal to k
.
Example 1:
Input: s = "aaabb", k = 3 Output: 3 Explanation: The longest substring is "aaa", as 'a' is repeated 3 times.
Example 2:
Input: s = "ababbc", k = 2 Output: 5 Explanation: The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
Constraints:
1 <= s.length <= 10^4
s
consists of only lowercase English letters.1 <= k <= 10^5
思路:
题意大概是找到最长字串,子串中每个字母重复的次数不少于k。采用分而治之的方法。首先用hash map记录下每个字母出现的次数,然后去遍历字符串,这里要反着想,不是找符合条件的字母,而是去找不符合条件的字母,这样不符合条件的字母就成了我们分而治之的断点。在递归中,如果当前字符串都满足条件,则直接返回长度,否则找到断点后继续递归左边和右边。这里分段有两个点要注意,一是分段的那个点可以不被包括在左边和右边,因为它本身就不符合k;二是在找到第一个断点后,递归左边,这时候再往下遍历,找到第一个符合k的字母再分段给右边,如果不加这个第二点,最后一个case会超时。
代码:
class Solution {
public:
int longestSubstring(string s, int k) {
if(s.size()<k)
return 0;
unordered_map<char,int> record;
for(auto i:s)
record[i]++;
bool found=true;
int index;
for(int i=0;i<s.size();i++)
{
if(record[s[i]]<k)
{
found=false;
index=i;
break;
}
}
if(found)
return s.size();
int left=longestSubstring(s.substr(0,index),k);
while(index<s.size()&&record[s[index]]<k)
index++;
int right=longestSubstring(s.substr(index),k);
return max(left,right);
}
};