Longest Substring with At Most Two Distinct Characters

  1. # Given a string, find the length of the longest substring T that contains at most 2 distinct characters.
  2. # For example, Given s = “eceba”,
  3. # T is "ece" which its length is 3.
  4. # Hide Company Tags Google
  5. # Hide Tags Hash Table Two Pointers String
  6. # Hide Similar Problems (M) Longest Substring Without Repeating Characters (H) Sliding Window Maximum (H) Longest Substring with At Most K Distinct Characters
  7. class Solution(object):
  8. def lengthOfLongestSubstringTwoDistinct(self, s):
  9. """
  10. :type s: str
  11. :rtype: int
  12. """
  13. char1, right1, char2, right2 = '', 0, '', 0
  14. start = 0
  15. longest = 0
  16. for i, c in enumerate(s):
  17. if not char1 or char1==c:
  18. char1=c
  19. right1=i
  20. elif not char2 or char2==c:
  21. char2=c
  22. right2=i
  23. else:
  24. # print i
  25. if right1<right2:
  26. char1 = c
  27. start=right1+1
  28. right1 = i
  29. else:
  30. char2 = c
  31. start=right2+1
  32. right2 = i
  33. #print 'i={},start={},right1={},righ2={}'.format(i,start,right1,right2)
  34. longest = max(longest, i-start+1)
  35. return longest
  36. sol = Solution()
  37. assert sol.lengthOfLongestSubstringTwoDistinct("cdaba")==3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. js限制input输入

    1.取消按钮按下时的虚线框,在input里添加属性值 hideFocus 或者 HideFocus=true <input type="submit" value=" ...

  2. 线段树或树状数组---Flowers

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...

  3. 认识Python

    web框架:Django.Tornado.Flask Twisted:复杂的异步网络框架 指定解释器 #!/usr/bin/env python #!/usr/bin/python print (&q ...

  4. 通过NameValuePairsValueProvider对象来获取指定前缀的Key

    using System; using System.Collections.Generic; using System.Linq; using System.Web.Http.ValueProvid ...

  5. GetStartedWithWin10Develop

    GetStartedWithWin10Develop 首先要确保已经配置好win10开发环境,开始第一个win10开发的HelloWorld 1.首先创建你的win10项目(示例的项目名称为 Hell ...

  6. MySQL数据库中字符集的问题

    今天在做Hibernate案例,往mysql中写记录的时候,出现ERROR: Incorrect string value: '\xE5\x8A\xA0\xE5\x86\x85...' for col ...

  7. Oracle SQL Tips

    左连接的同时只输出关联表的一条记录 WITH X AS (SELECT 1 ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL ...

  8. 操作集合的工具类:Collections

    Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类提供了大量方法对集合进行排序.查询和修改等操作,还提供了将集合对象置为不可变.对集合对象实现同步控制等方法 ...

  9. [android] 手机卫士自定义控件的属性

    上一节完成的自定义组合控件,灵活性不够,控件的显示信息上,仿照系统属性,自定义自己的属性 上一节组合控件SettingItemView中有三个控件,分别是TextView大标题,TextView描述, ...

  10. Cocos2d入门--3--向量的应用

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...