LeetCode 395. Longest Substring with At Least K Repeating Characters

原题链接在这里:https://leetcode.com/problems/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.

题解:

To make sure every char in substring appears no less than k time, we need to make sure unique count == no less than k count.

We could add one more argument uniTargetCnt, that is the target of count of unique char.

First move the runner.

While unique count > target, move the walker.

Update the longest with target unique count.

target unique count could be [1, 26].

Time Complexity: O(n). n = s.length().

Space: O(1).

AC Java:

 1 class Solution {
 2     public int longestSubstring(String s, int k) {
 3         if(s == null || s.length() == 0 || k <= 0){
 4             return 0;
 5         }
 6
 7         int res = 0;
 8         for(int i = 1; i <= 26; i++){
 9             res = Math.max(res, longestTargetSubstring(s, k, i));
10         }
11
12         return res;
13     }
14
15     private int longestTargetSubstring(String s, int k, int uniTargetCnt){
16         int walker = 0;
17         int runner = 0;
18         int [] map = new int[26];
19         int uniCnt = 0;
20         int overKCnt = 0;
21         int res = 0;
22
23         while(runner < s.length()){
24             if(map[s.charAt(runner) - ‘a‘]++ == 0){
25                 uniCnt++;
26             }
27
28             if(map[s.charAt(runner++) - ‘a‘] == k){
29                 overKCnt++;
30             }
31
32             while(uniCnt > uniTargetCnt){
33                 if(map[s.charAt(walker) - ‘a‘]-- == k){
34                     overKCnt--;
35                 }
36
37                 if(map[s.charAt(walker++) - ‘a‘] == 0){
38                     uniCnt--;
39                 }
40             }
41
42             if(uniCnt == uniTargetCnt && uniCnt == overKCnt){
43                 res = Math.max(res, runner - walker);
44             }
45         }
46
47         return res;
48     }
49 }

类似Minimum Window Substring.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12155767.html

时间: 2024-08-03 07:05:42

LeetCode 395. Longest Substring with At Least K Repeating Characters的相关文章

LeetCode 395. Longest Substring with At Least K Repeating Characters C#

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"

395. Longest Substring with At Least K Repeating Characters

https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/87739/Java-Strict-O(N)-Two-Pointer-Solution window分别为1-26个unique的字母,找出为这个长度的字母的 其中至少有K repeating ones的最长子序列,取最大值 1 class Solution { 2 public int longestSubst

395 Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子串

找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k .输出 T 的长度.示例 1:输入:s = "aaabb", k = 3输出:3最长子串为 "aaa" ,其中 'a' 重复了 3 次.示例 2:输入:s = "ababbc", k = 2输出:5最长子串为 "ababb" ,其中 'a' 重复了 2 次, 'b' 重复了 3 次. 详见:https://leetcode.com

[LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

Given a string, find the length of the longest substring T that contains at most k distinct characters. For example, Given s = “eceba” and k = 2, T is "ece" which its length is 3. 159. Longest Substring with At Most Two Distinct Characters 的拓展,1

Leetcode: 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"

[LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

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"

[LeetCode] 340. Longest Substring with At Most K Distinct Characters

Given a string, find the length of the longest substring T that contains at most k distinct characters. Example 1: Input: s = "eceba", k = 2 Output: 3 Explanation: T is "ece" which its length is 3. Example 2: Input: s = "aa",

leetcode395 Longest Substring with At Least K Repeating Characters

思路: 尺取法. 循环i:1~26,分别计算恰好包含i种字母并且每种字母出现的次数大于等于k个的最长子串长度. 没法直接使用尺取法,因为不满足区间单调性,但是使用如上的方法却是可以的,因为子串中包含的字母种类数是满足区间单调性的. 实现: 1 #include <bits/stdc++.h> 2 using namespace std; 3 class Solution 4 { 5 public: 6 int longestSubstring(string s, int k) 7 { 8 in

340. Longest Substring with At Most K Distinct Characters

/* * 340. Longest Substring with At Most K Distinct Characters * 2016-7-10 by Mingyang * 利用HashMap来做sliding window的做法非常好! */ public int lengthOfLongestSubstringKDistinct(String s, int k) { Map<Character, Integer> map = new HashMap<>(); int lef