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: 5
Explanation: tis "aabbb" which its length is 5. Time: O(N)
Space: O(N)
 class Solution:
def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
res = 0
start, end, count = 0, 0, 0
my_dict = {}
while end < len(s):
end_char = s[end]
end_freq = my_dict.get(end_char, 0)
my_dict[end_char] = end_freq + 1
if end_freq == 0:
count += 1
end += 1
// cannot use count <= 2 to get res b/c end will run before start
while count > 2:
start_char = s[start]
start_freq = my_dict[start_char]
my_dict[start_char] = start_freq - 1
if start_freq == 1:
count -= 1
start += 1
res = max(res, end - start)
return res

[LC] 159. Longest Substring with At Most Two Distinct Characters的更多相关文章

  1. [LeetCode] 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 char ...

  2. 【LeetCode】159. Longest Substring with At Most Two Distinct Characters

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...

  3. ✡ 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 characte ...

  4. [leetcode]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 char ...

  5. 159. Longest Substring with At Most Two Distinct Characters

    最后更新 二刷 08-Jan-17 回头看了下一刷的,用的map,应该是int[256]的意思,后面没仔细看cuz whatever I was doing at that time.. wasnt ...

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

    找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...

  7. [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 characte ...

  8. [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 characte ...

  9. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

随机推荐

  1. 学生信息的添加 Java web简单项目初试(修改)

    错误原因: 1.Java web 的Servlet类没有配置好,并且缺少一个 Dao类(Date Access Object通常用于操作数据库的). 2.代码的某些名称错误,导致数据库数据存储错误. ...

  2. OSS 图片处理流程

    1.步骤一 2.步骤二 3.步骤三 4.步骤四 5.步骤五(步骤4完成会自动添加cname用户解析,不需要自己去加,只需要点击进来看下是否添加成功即可) 通过以上步骤就可以实现了图片服务的配置

  3. 安装eclipse步骤以及配置jdk

    1.官网下载eclipse 2.下载jdk并且安装,记住自己安装路径 3.配置jdk环境变量 在高级系统设置里面配置 新建: 用户变量:“变量名”:JAVA_HOME “变量值”:C:\Program ...

  4. mysql数据库大规模数据读写并行时导致的锁表问题

    问题介绍 最近在给学校做的一个项目中,有一个功能涉及到考核分数问题. 我当时一想,这个问题并不是很难,于是就直接采用了这样的方法:拿着一个表中的数据作为索引,去挨个遍历相关表中的数据,最后经过算分的过 ...

  5. 洛谷 P1341 无序字母对(欧拉回路)

    题目传送门 解题思路: 一道欧拉回路的模板题,详细定理见大佬博客,任意门 AC代码: #include<cstdio> #include<iostream> using nam ...

  6. SQL 一个【OR】属性给你带来了怎样的麻烦

    关于 数据库  or   的用法,就不说了     讲讲OR对数据库性能有多大的杀伤力,以后线上的项目如果是数据量很大的表,一定不要用OR来查询 今天在工作的时候,同事遇到了SQL优化的问题,让我帮忙 ...

  7. UML-为什么要使用层?

    1.内聚职责:使关系分离.减少耦合和依赖,提高潜在复用性. 2.领域层和技术服务层可以是分布式的 3.利于团队开发

  8. Maven--仓库的分类

    对于 Maven 仓库来说,仓库只分为两类:本地仓库和远程仓库. 当 Maven 根据坐标寻找构件的时候,它首先会查看本地仓库,如果本地仓库存在此构件,则直接使用:如果本地仓库不存在此构件,或者需要查 ...

  9. Django框架(一):MVC设计模式、Django简介

    1. MVC设计模式 MVC设计模式:Model-View-Controller简写. 最早由TrygveReenskaug在1978年提出,是施乐帕罗奥多研究中心(Xerox PARC)在20世纪8 ...

  10. 好看的UI组合,为以后自己写组件库做准备

    1. 黑色格子背景 { color: rgb(255, 255, 255); text-shadow: 1px 1px 0 rgba(0,0,0,.3); rgb(62, 64, 74); backg ...