首页 > 其他分享 >340. Longest Substring with At Most K Distinct Characters 最多k个字母不同的字符串

340. Longest Substring with At Most K Distinct Characters 最多k个字母不同的字符串

时间:2022-11-12 02:22:06浏览次数:45  
标签:map Most Distinct leftChar Substring int length best left

Given a string s and an integer k, return the length of the longest 

substring  of s that contains at most k distinct characters.

 

 

Example 1:

Input: s = "eceba", k = 2
Output: 3
Explanation: The substring is "ece" with length 3.

Example 2:

Input: s = "aa", k = 1
Output: 2
Explanation: The substring is "aa" with length 2.

public int lengthOfLongestSubstringKDistinct(String s, int k) {
    Map<Character, Integer> map = new HashMap<>();
    int left = 0;
    int best = 0;
    for(int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        map.put(c, map.getOrDefault(c, 0) + 1);
        while (map.size() > k) {
            char leftChar = s.charAt(left);
            if (map.containsKey(leftChar)) {
                map.put(leftChar, map.get(leftChar) - 1);                     
                if (map.get(leftChar) == 0) { 
                    map.remove(leftChar);
                }
            }
            left++;
        }
        best = Math.max(best, i - left + 1);
    }
    return best;
} 

 

 

标签:map,Most,Distinct,leftChar,Substring,int,length,best,left
From: https://www.cnblogs.com/immiao0319/p/16882582.html

相关文章

  • C++'s most vexing parse
    本文地址https://www.cnblogs.com/wanger-sjtu/p/16876846.htmlC++'smostvexingparse是ScottMeyers在其名著《EffectiveSTL》中创造的一个术语。Scott用这个术......
  • POJ 1226 Substrings
    DescriptionYouaregivenanumberofcase-sensitivestringsofalphabeticcharacters,findthelargeststringX,suchthateitherX,oritsinversecan......
  • ZOJ 2132 the most frequent number
    DescriptionSeven(actuallysix)problemsmaybesomewhatfewforacontest.ButIamreallyunabletodeviseanotherproblemrelatedtoFantasyGameSeries.......
  • SPOJ LCS Longest Common Substring
    DescriptionAstringisfinitesequenceofcharactersoveranon-emptyfinitesetΣ.Inthisproblem,Σisthesetoflowercaseletters.Substring,alsocalled......
  • SPOJ LCS2 Longest Common Substring II
    DescriptionAstringisfinitesequenceofcharactersoveranon-emptyfinitesetΣ.Inthisproblem,Σisthesetoflowercaseletters.Substring,alsocalled......
  • HUST 1602 Substring
    DescriptionThisproblemisquieteasy. Initially,thereisastringA.   Thenwedothefollowingprocessinfinitytimes.  A:=A+......
  • SPOJ 705 New Distinct Substrings
    DescriptionGivenastring,weneedtofindthetotalnumberofitsdistinctsubstrings.InputT-numberoftestcases.T<=20;Eachtestcaseconsistsofonestr......
  • HDU 1403 Longest Common Substring
    ProblemDescriptionGiventwostrings,youhavetotellthelengthoftheLongestCommonSubstringofthem.Forexample:str1=bananastr2=ciana......
  • Computer Vision_33_SIFT:PCA-SIFT A More Distinctive Representation for
    此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面。对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多......
  • Substring 在BCL和CLR里面搞了啥
    楔子还是做点事情,不要那么散漫。本文以简单的Substring(intstartindex,intLength)函数为例,来递进下它在托管和非托管的一些行为。以下均为个人理解,如有疏漏请指正。......