[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 ...
随机推荐
- JKS not Found
近期使用Spring Boot开发微信验证的时候, 在获取token时,Idea老是提示Jks not found,网上找资料,都说是SSL的问题 实际解决方法: 重装JDK,将JDK重装之后,运行正 ...
- vue每次运行起来端口不一致问题
原因:portfinder新发布的版本异常 解决方案:npm install portfinder@1.0.21
- javascript编程中极易出现的错误(个人)
2018-08-10 1,setInterval打错字写成ser 2,document.getElementById().innerHTML;HTML需要全部大写 3,在for循环中定义一个i时要记住 ...
- 吴裕雄--天生自然 PHP开发学习:MySQL 插入数据
<?php $servername = "localhost"; $username = "root"; $password = "admin& ...
- Spring Cloud服务间调用鉴权
学习使用Spring Cloud 微服务间的调用都是RestFul风格,如何保证调用之间的安全性,这是一个很重要的问题. 通过查阅资料http://wiselyman.iteye.com/blog/2 ...
- 我是如何从Java转型为Go区块链工程师
我是如何从Java转型为Go区块链工程师 本文来自于一个比原链核心开发的陈述 前言 IT部落在加入比原链之前一直是做Java开发的,当初看到Go还有点犹豫,还怕过不了面试,结果是否掌握一门语言的考量确 ...
- Python 中 JSON和dict的转换,json的使用
一. 基础语法 在Python 的 json库中,共有四个方法.分别是: json.load() # 从文件中加载 json.loads() # 数据中加载 json.dump() # 转存到文件 j ...
- drf_jwt手动签发与校验-drf小组件:过滤-筛选-排序-分页
签发token 源码的入口:完成token签发的view类里面封装的方法. 源码中在请求token的时候只有post请求方法,主要分析一下源码中的post方法的实现. settings源码: 总结: ...
- uboot对Flash和DDR的分区管理
1.uboot阶段对Flash的分区 (1).所谓分区,就是对Flash进行分块管理. (2).PC机等产品中,因为大家都是在操作系统下使用硬盘的,整个硬盘由操作系统统一管理,操作系统会使用文件系统帮 ...
- #JS# 如何判断一个字符串是否为日期格式
var data = “2018-12-09”; //返回为false则是日期格式;isNaN(data)排除data为纯数字的情况(此处不考虑只有年份的日期,如‘2018’) if(isNaN(da ...