Longest Substring with At Least K Repeating Characters(区别)

Find the length of the longest substringTof a given string (consists of lowercase letters only) such that every character inTappears no less thanktimes.

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.

双指针

双指针做26次,1-27每次代表有几个unique char,保证每个unique char的frequency 》= k.

每次双指针, s, e记录2个 # of unique chars && # of char more than K

如果这俩# == 当前数字 1-27, 是个可行解,比较计算max。

用26个位向量,2个loop,头固定尾动。

每个新头新counter和新mask。出现了但不到k的char设为1,满K设为0.最后mask ==0 说明所有出现的char都出现K次,得到一个解。

注意~(1<<i)按位取反

超时了本作法 https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/87749/Two-short-C%2B%2B-solutions-(3ms-and-6ms)

双指针模板没做出来,用递归

找出count<k的char,用来做分隔符把str分成2段,继续递归。最后记得返回len(s)

Last updated

Was this helpful?