leetcode 395 Longest Substring with At Least K Repeating Characters

时间:2019-06-12
本文章向大家介绍leetcode 395 Longest Substring with At Least K Repeating Characters,主要包括leetcode 395 Longest Substring with At Least K Repeating Characters使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

lc395  Longest Substring with At Least K Repeating Characters

思路是按子串中不同字母的个数来计算,1~26

每次计算n个不同字母所能构成的最长子串

记录其中最长子串

单次循环中,用双指针end向后遍历s,begin用来计算子串长度

当见过的字母个数 == n且这些字母出现次数都大于等于k

说明begin~end这个子串符合条件,更新maxLen

若 >,则需要向后移动begin,使其变为==

若<,不需要操作

 1 class Solution {
 2     public int longestSubstring(String s, int k) {
 3         if(s.length() == 0 ||s.length() < k)
 4             return 0;
 5         int max = 0;
 6         for(int i=1; i<=26; i++){
 7             max = Math.max(max, helper(s, k, i));
 8         }
 9         
10         return max;
11     }
12     
13     private int helper(String s, int k, int abcNum){
14         int[] count = new int[128];
15         int begin = 0, end = 0;
16         int seenChar = 0, noLessThanK = 0;
17         int res = 0;
18         
19         while(end < s.length()){
20             if(count[s.charAt(end)]++ == 0)
21                 seenChar++;
22             if(count[s.charAt(end++)] == k)
23                 noLessThanK++;
24             
25             /*while(seenChar > abcNum){
26                 if(count[s.charAt(begin)]-- == k)
27                     noLessThanK--;
28                 if(count[s.charAt(begin++)] == 0)
29                     seenChar--;
30             }*/
31             while(seenChar > abcNum){
32                 if(count[s.charAt(begin)]-- == 1)
33                     seenChar--;
34                 if(count[s.charAt(begin++)] == k-1)
35                     noLessThanK--;
36             }
37             
38             if(seenChar == noLessThanK && seenChar == abcNum)
39                 res = Math.max(res, end-begin);
40         }
41         
42         return res;
43     }
44 }

原文地址:https://www.cnblogs.com/hwd9654/p/11009640.html