395. Longest Substring with At Least K Repeating Characters

Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.

Example 1:

Input:
s = "aaabb", k = 3

Output:
3

The longest substring is "aaa", as 'a' is repeated 3 times.

Example 2:

Input:
s = "ababbc", k = 2

Output:
5

The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.

 题目的意思是找最长的子字符串T,在T中每个字母出现的次数都不少于k,返回结果是T的长度。

思路:

一,用HashMap找到每个字母出现的次数;

二,如果每个字母出现的次数都不小于k,那么直接返回s的长度即可,否则,如果一个字母出现次数小于k,那T一定不包含这个字符,那么就可以以出现次数小于k的字母作为分隔符;

三,将分割后的子字符串重复步骤一,二。

主要参考了这篇博客。代码如下:

//map用于记录每个character出现的次数
		Map map = new HashMap<>();
		for(int i=0;i
PS:很多天没写博客了啊==

你可能感兴趣的:(LeetCode)