395 Longest Substring with At Least K Repeating Characters
1. Question
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.2. Implementation
(1) Two Pointers + Hash
思路: 由于题目说了string只包含lowercase的字母,所以我们可以列举包含1个字母到包含全部26个字母情况下,找出满足条件的最长子串.其中unique代表不同的字母个数,atLeastK代表在子串中重复出现的次数至少k次的字母的个数
(2) Divide and Conquer
3. Time & Space Complexity
Two Pointers + Hash: 时间复杂度O(26 * n), 空间复杂度O(1)
Divide and Conquer: 时间复杂度O(n^2), 空间复杂度O(n)
Last updated
Was this helpful?