zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    leetcode 62. Unique Paths
    leetcode 345. Reverse Vowels of a String
    leetcode 344. Reverse String
    Centos7.4 kafka集群安装与kafka-eagle1.3.9的安装
    yarn调度器 FairScheduler 与 CapacityScheduler
    Hive性能优化
    HBase笔记
    Zookeeper笔记
    Hadoop组件详解(随缘摸虾)
    ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布式集群搭建
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12155767.html
Copyright © 2011-2022 走看看