Longest Substring with At Most K Distinct Characters

要点:要搞清楚At Most Two Distinct和Longest Substring Without Repeating Characters (No Repeating)的区别:前者的sliding window里只有2个char,但是可以任意重复。而后者可以有任意多个char但是任何char都不能有重复。

  • 所以解法上前者的hashset只有2个元素,而后者需要把所有distinct的元素放到hashset里。而前者因为只有当不在hashset中的元素才可能考虑更新hashset,而后者是有当前元素在hashset里更新。
  • 推广到k,
    • At Most K Distinct要把k个元素中最后一次出现(最右)最靠左的那个去掉,所以要不断更新最右边界,At Most K Repeating因为新元素在hashset中,所以去掉的只能是该元素,只是k扩展到要记录重复的元素个数是否到k才启动。
    • At Most K Distinct还有用count的方法:把集中检查边界分布到从左向右每个元素减少count,最先count减少到0的就是最左。而At Most K Repeating因为只去掉一个元素,没有这个方法。

要点:

  • 是对longest substring without repeating characters这题另一种思路的扩展。no repeating这题是用map存当前sliding window的字符,下一个char不能出现在map中。而distinct这题是下一个不能是没有在map中的(除非map中只有一个or <k个)。
  • k个中选哪个取代?显然k个字符中最后出现中最靠左的字符是所选。这样能保证当前sliding window中的local maxLen
  • 上面的方法每次新字符都要遍历k个在map中的字符,整体时间是O(n)*O(k)。另一种方法更类似no repeating的方法。直接从sliding window左边开始pop直到某个元素count==0(所以map中记录count)。其实也是找到最后出现的最靠左字符。

错误点:

  • start的更新在清楚左边界的loop内,如果没有进入这个清除环节,不需要更新start
  • start的更新在leftMost的+1位置
  • count version不用检查>0,因为在map中的都是如此
  • count version:注意start是index,umap[s[start]]
  • count version: 初始map中的count value是1而不是0
  • count version: 别忘了新元素进map(主要是光想着del key了)

https://repl.it/CaiH (map loop)
https://repl.it/CaiT/1 (k, count)
https://repl.it/CqmA (Two, use char1, char2 variables)

class Solution(object):
    def lengthOfLongestSubstringKDistinct(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        n = len(s)
        maxLen = 0
        umap = {}
        start = 0
        for i in xrange(n):
            if s[i] not in umap and len(umap)>=k:
                leftMost = n
                rmChar = None
                for c in umap:
                    if umap[c]<leftMost:
                        leftMost = umap[c]
                        rmChar = c
                del umap[rmChar]
                start = leftMost+1 # error 1: start should be set inside update condition
                                   # error 2: start index: next of leftMost
            umap[s[i]]=i

            if i-start+1>maxLen:
                maxLen = i-start+1
            #print i,start,maxLen,umap
        return maxLen

sol = Solution()
print sol.lengthOfLongestSubstringKDistinct("aabbcc", 1)
print sol.lengthOfLongestSubstringKDistinct("aabbcc", 2)
print sol.lengthOfLongestSubstringKDistinct("aabbcc", 3)
print sol.lengthOfLongestSubstringKDistinct("eceba", 2)
        
# 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.

# Hide Company Tags Google
# Hide Tags Hash Table String
# Hide Similar Problems (H) Longest Substring with At Most Two Distinct Characters

class Solution(object):
    def lengthOfLongestSubstringKDistinct(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        umap = {}
        if k==0: return 0
        longest, start = 0,0
        for i in xrange(len(s)):
            # print "it=", i,umap,len(umap)
            if s[i] in umap:
                umap[s[i]]+=1
            elif len(umap)<k:
                umap[s[i]]=1 # error 1: should be 1, not 0
            else:
                umap[s[i]]=1 # error 2: don't forget to put myself into
                while start<i:
                    c = s[start]
                    start+=1
                    umap[c]-=1
                    if not umap[c]:
                        del umap[c]
                        break
            longest = max(longest, i-start+1)
        return longest

sol = Solution()
assert sol.lengthOfLongestSubstringKDistinct("eceba", 2)==3
        

边工作边刷题:70天一遍leetcode: day 71-1的更多相关文章

  1. 边工作边刷题:70天一遍leetcode: day 71

    Longest Substring with At Most Two Distinct Characters # Given a string, find the length of the long ...

  2. 边工作边刷题:70天一遍leetcode: day 89

    Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...

  3. 边工作边刷题:70天一遍leetcode: day 77

    Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...

  4. 边工作边刷题:70天一遍leetcode: day 78

    Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...

  5. 边工作边刷题:70天一遍leetcode: day 85-3

    Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...

  6. 边工作边刷题:70天一遍leetcode: day 101

    dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...

  7. 边工作边刷题:70天一遍leetcode: day 1

    (今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...

  8. 边工作边刷题:70天一遍leetcode: day 70

    Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...

  9. 边工作边刷题:70天一遍leetcode: day 71-3

    Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...

  10. 边工作边刷题:70天一遍leetcode: day 71-2

    One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...

随机推荐

  1. [moka同学笔记]yii2场景的使用(摘录)

    前半部分为自己使用的过程,下边为转载的,具体地址见:http://blog.sina.com.cn/s/blog_88a65c1b0101j717.html 1.在model中 public func ...

  2. C语言回滚(二)--循环打印

    //1.用循环打印 /* FFEFEDFEDCFEDCBFEDCBA */ #include <stdio.h> #include<stdlib.h> int main(){ ...

  3. If you insist running as root, then set the environment variable RUN_AS_USER=root...

    版权声明:本文为博主原创文章,不经博主允许注明链接即可转载. If you insist running as root, then set theenvironment variable RUN_A ...

  4. copy 和 strong(或retain)的区别

    http://stackoverflow.com/questions/18526909/whether-i-should-use-propertynonatomic-copy-or-propertyn ...

  5. SharePoint 2013 中自定义WCF服务

    在使用SharePoint2013的时候,如果其他客户端 API 的组合不足,可以通过自定义 Web 服务扩展 SharePoint.默认情况下,SharePoint 2013 不仅支持创建自定义 A ...

  6. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q35-Q39)

    Question 35You have a custom Web Part that is deployed as a sandboxed solution.You need to ensure th ...

  7. Android拍照保存在系统相册不显示的问题

    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(new File(& ...

  8. UITabBarController QQ

    AppDelegate.m #import "AppDelegate.h" #import "FirstViewController.h" #import &q ...

  9. IOS Quartz2D简介

    Quartz2D 简介( 后续会有相关应用) 第一部分 绘制直线 代码示例: - (void)drawRect:(CGRect)rect{ //获取图形上下文 CGContextRef cxConte ...

  10. iOS学习笔记10-UIView动画

    上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...