159 Longest Substring with At Most Two Distinct Characters

这两题有一个 trick 和 Minimum Window Substring 非常
像,就是维护一个 "curCount" 代表目前 (i,j) 之间 match 上
的数量,而通过 hash[] 的正负充当计数器的作用

public int lengthOfLongestSubstringTwoDistinct(String s) {
    int maxSize = 0;
    int j = 0;
    int[] hash = new int[256];
    int distinctCount = 0;
    for(int i = 0; i < s.length(); i++){
        while(j < s.length()){
            if(distinctCount == 2 && hash[s.charAt(j)] == 0)
                break;
            if(hash[s.charAt(j)] == 0) distinctCount ++;
            hash[s.charAt(j++)]++;
        }
        if(j - i > maxSize){
            maxSize = j - i;
        }
        hash[s.charAt(i)]--;
        if(hash[s.charAt(i)] == 0) distinctCount --;
    }
    return maxSize;
}

  

时间: 2024-10-15 13:42:10

159 Longest Substring with At Most Two Distinct Characters的相关文章

leetcode 159. Longest Substring with At Most Two Distinct Characters 求两个字母组成的最大子串长度 --------- java

Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example, Given s = "eceba", T is "ece" which its length is 3. 给一个字符串,求这个字符串中,由两个字母组成的,连续的最长子串的长度. 虽然是hard,但是感觉并没有什么难度. 用ch1和pre

[LC] 159. Longest Substring with At Most Two Distinct Characters

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

leetcode[159]Longest Substring with At Most Two Distinct Characters

找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 例如:string s="aqaqedadcdccd"; 使用一个map<char,int>  fmap记录每个字符出现的个数,详解在程序中注释. int lengthOfLongestSubstringTwoDistinct(string s) { int begin=0,size=0,res=0; map<char,int> fmap; for (int i=0;

[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 &quot;Longest Substring with At Most K Distinct Characters&quot;

A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical sliding window problem. class Solution { public: int lengthOfLongestSubstringKDistinct(string s, int k) { unordered_map<char, unsigned> hm; int ret =

[LeetCode] 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. 这道题是之前那道Longest Substring with At Most Two Distinct Characters的拓展

【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)

Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example, Given s = “eceba”, T is "ece" which its length is 3. 这个题很显然使用双指针进行遍历的,beg

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

Longest Substring with At Most K Distinct Characters

Given a string, find the longest substring that contains only two unique characters. For example, given "abcbbbbcccbdddadacb", the longest substring that contains k unique character is "bcbbbbcccb". 分析: 用hashmap记录每个character从start到当前位置