[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 ...
随机推荐
- 干货 | TiDB Operator实践
干货 | TiDB Operator实践 北京it爷们儿 京东云开发者社区 4天前 K8s和TiDB都是目前开源社区中活跃的开源产品,TiDB Operator项目是一个在K8s上编排管理TiDB集 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL UNION 操作符
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中.多个 SELECT 语句会删除重复的数据. 语法 MySQL UNION 操作符语法格式: SELECT ...
- Neo4j安装配置(mac)
Neo4j安装配置(mac) 1.下载APP 注意:无需配置变量 下载地址:https://neo4j.com/download/ 2.安装程序并启动 3.创建数据库(local) 选择版本 4.启动 ...
- Java学习十六
学习内容: 1.做毕设 2.Java异常类 3.Java包装类 1.System.exit(1):终止程序运行,终止final执行方法 2.throws抛出异常类型,throw抛出异常对象 用法:th ...
- Linux--wget,yum,rpm,apt-get
参考:http://blog.csdn.net/ziju125521/article/details/52575715 使用wget下载一个 rpm包, 然后用 rpm -ivh xxx.rpm ...
- Java--定时
TimerTask task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub ...
- Vue.js——2.第一个Vue程序
代码 <div id="app"> <p>{{msg}}</p> </div> <script> let vm=new ...
- APP-计算器
在网课上学习了计算器的制作方法,并自己做了小常识,看完一便后,感觉自己会了,思想也明白了,但是当关掉视频真正开始做的时候会发向许多的问题,自己在前的好多语法用的并不是很熟练,但是反复的查阅资料,观看教 ...
- C语言-再论指针与数组
指针与数组的天生姻缘1.以指针方式来访问数组元素(1).数组元素使用时不能整体访问,只能是单个访问.访问形式有两种:数组形式和指针形式.(2).数组形式访问数组元素:数组名[下标]:(下标从0开始(3 ...
- C语言-字、半字、内存位宽相关
1.32位系统:32位系统指的是32位数据线,但是一般地址线也是32位,这个地址线32位决定了内存地址只能有32位二进制,所以逻辑上的大小为2的32次方.内存限制就为4G.实际上32位系统中可用的内存 ...