[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: 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的更多相关文章
- [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 ...
- 【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 ...
- ✡ 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 ...
- [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 ...
- 159. Longest Substring with At Most Two Distinct Characters
最后更新 二刷 08-Jan-17 回头看了下一刷的,用的map,应该是int[256]的意思,后面没仔细看cuz whatever I was doing at that time.. wasnt ...
- leetcode[159] Longest Substring with At Most Two Distinct Characters
找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- java线程——线程局部变量
一,线程局部变量ThreadLocal的作用 用于实现线程内部的数据共享,既对于相同的程序代码,多个模块在同一个线程中运行时要共享一份数据,在另一个线程访问的时候,访问的由是另一份数据. 每个线程调用 ...
- 2020/1/28 PHP代码审计之代码执行漏洞
0x00代码执行原理 应用程序在调用一些能够将字符串转换为代码的函数(如PHP中的eval)时,没有考虑用户是否控制这个字符串,将造成代码执行漏洞. 该漏洞主要存在于eval().assert().p ...
- github新建一个单页
比如可以在github上打开的网页是这种网址形式的:https://01xunsicheng.github.io/yumeihua/ 1.登录后首页找到 New repository 2.新建一个文件 ...
- Python笔记_第四篇_高阶编程_正则表达式_1.正则表达式简介(re模块)
1. 从一个判断手机号的问题引入: 如果给你一个字符串,去判断是否是一个手机号码,我们通过之前的学习可以有如下代码: # 如果用普通的方式去检验一个电话号码非常麻烦. def checkPhone(s ...
- POJ - 1061 扩展欧几里德算法+求最小正整数解
//#pragma comment(linker, "/STACK:1024000000,1024000000") //#pragma GCC optimize(2) #inclu ...
- CentOS8上用Docker部署开源项目Tcloud
一.安装Docker 1.我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核3.8以上) root账户登录,查 ...
- This inspection highlights chained comparisons that can be simplified.
https://stackoverflow.com/questions/26502775/pycharm-simplify-chained-comparison In Python you can & ...
- UVA 10806 最小费用最大流
终于可以写这道题的题解了,昨天下午纠结我一下下午,晚上才照着人家的题解敲出来,今天上午又干坐着想了两个小时,才弄明白这个问题. 题意很简单,给出一个无向图,要求从1 到 n最短路两次,但是两次不允许经 ...
- VMware vSphere虚拟化-VMware ESXi 5.5组件添加本地磁盘--虚拟机扩容
本地存储器可以是位于ESXi主机内部的内部硬盘,也可以是位于主机之外并直接通过SAS或者SATA等协议连接在主机上的外部存储系统.本地存储不需要存储网络即可与主机进行通信,只需要一根连接到存储单元的电 ...
- 【MySQL参数】-innodb_flush_method
innodb_flush_method这个参数控制着innodb数据文件及redo log的打开.刷写模式,对于这个参数,文档上是这样描述的:有三个值:fdatasync(默认),O_DSYNC,O_ ...