LintCode 386: Longest Substring with At Most K Distinct Characters (同向双指针经典题!!!)

  1. Longest Substring with At Most K Distinct Characters
    Given a string s, find the length of the longest substring T that contains at most k distinct characters.

Example
For example, Given s = “eceba”, k = 3,

T is “eceb” which its length is 4.

Challenge
O(n), n is the size of the string s.

思路:同向双指针。和LintCode 406 (Minimum-Size-Subarray-Sum) 有点象。都是right指针先动,符合条件后,左指针再动,动的过程中必须满足条件。
但区别是406的while()是遍历符合条件(即sum>=s)的left,所以minLen的更新在while()之内。
这题的while()是筛掉不符合条件(即count > k)的left,所以maxLen的更新在while()之外。
两个条件都不需要加l<r或l<=r。

class Solution {
public:
    int lengthOfLongestSubstringKDistinct(string s, int k) {
        int maxLen = 0;
        int l = 0, r = 0;
        int len = s.size();
        if (len == 0 || k == 0) return 0;
        
        vector<int> dict(256, 0);
        int count = 0;
        
        for (int r = 0; r < len; ++r) {
            ++dict[s[r]];
            if (dict[s[r]] == 1) count++;
            
            while (count > k) {
                dict[s[l]]--;
                if (dict[s[l]] == 0) count--;
                ++l;
            }
            
            maxLen = max(maxLen, r - l + 1);
        }
        return maxLen;
    }
};

二刷:不如上次的代码好。

class Solution {
public:
    /**
     * @param s: A string
     * @param k: An integer
     * @return: An integer
     */
    int lengthOfLongestSubstringKDistinct(string &s, int k) {
        int len = s.size();
        int left = 0, right = 0;
        unordered_map<char, int> freq;
        int maxLen = 0;//, maxLenLeft = 0;
        while (left <= right && right < len) {
            freq[s[right]]++;
            if (freq.size() <= k) {
                if (maxLen < right - left + 1) {
                    maxLen = right - left + 1;
                }
            } 
            
            while (freq.size() > k) {
                if (left <= right) {
                    freq[s[left]]--;
                    if (freq[s[left]] == 0) freq.erase(s[left]);
                    left++;
                } else {
                    break;
                }
            }
            right++;  
        }
        return maxLen;
    }
};

三刷:这个模板好。

class Solution {
public:
    /**
     * @param s: a string
     * @param k: an integer
     * @return: the number of substrings there are that contain at least k distinct characters
     */
    long long kDistinctCharacters(string &s, int k) {
        int n = s.size();
        int left = 0, right = 0;
        vector<int> freq(26, 0);
        int windowCount = 0;
        long long res = 0;
        while (right < n) {
            char c = s[right] - 'a';
            if (freq[c] == 0) windowCount++;
            freq[c]++;
            right++;
            while (left < right && windowCount >= k) {
                res += n - right + 1;
                c = s[left] - 'a';
                freq[c]--;
                if (freq[c] == 0) windowCount--;
                left++;
            }
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值