博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Longest Substring with At Most Two Distinct Characters
阅读量:6250 次
发布时间:2019-06-22

本文共 2613 字,大约阅读时间需要 8 分钟。

Problem Description:

Given a string, find the length of the longest substring T that contains at most 2 distinct characters.

For example, Given s = “eceba”,

T is "ece" which its length is 3.

This problem has a very straight-forward solution. We maintain an unordered_set st to record the unique characters we have met, an integer len to record the length of the substring. 

Then we start to traverse the string from beinning to end. Each time we meet a character, if the size of st is less than 2 or its size is equal to 2 but the current character is in st (so adding it to the substring will still give only 2 distinct characters), we insert the character into st and increment len by 1.

The tricky case happens when the above underlined contition is violated. When it is violated, we know the substring needs to be changed. We reset len to 0 and clear st. Then we add the current character into st and increment len by 1. However, we cannot move forwards immediately now. We meed to move backwards to the beginning of the string and try to add as many characters as the underlined condition is not violated. Only after which we will move forwards as before.

If you feel confused, please consider the example "abcc". Once you add a and b into st and meet c, you clear st and add c into it. Then if you go forwards, you add the second c into s and you will conclude the longest length is 2. However, the answer is "bcc", which is of length 3. So after you add the first c, you need to move backwards and add b into st(you cannot move backwards and add a because it will violate the underlined condition), after which will you go forward to add the second c.

The code is as follows. You may try it on "abcc" to see how it works.

1     int lengthOfLongestSubstringTwoDistinct(string s) { 2         int len = 0, maxlen = 0; 3         unordered_set
st; 4 for (int i = 0; i < (int)s.length();) { 5 if (st.size() < 2 || (st.size() == 2 && st.find(s[i]) != st.end())) { 6 st.insert(s[i++]); 7 len++; 8 } 9 else {10 maxlen = max(maxlen, len);11 st.clear();12 int pos = i;13 st.insert(s[i--]);14 len = 1;15 while (i && (st.size() < 2 || (st.size() == 2 && st.find(s[i]) != st.end()))) {16 st.insert(s[i--]);17 len++;18 }19 i = pos + 1;20 }21 }22 return max(maxlen, len);23 }

转载于:https://www.cnblogs.com/jcliBlogger/p/4572538.html

你可能感兴趣的文章
人工智能深度学习Caffe框架介绍,优秀的深度学习架构
查看>>
JavaScript 九种跨域方式实现原理
查看>>
人工智能期末笔记
查看>>
ApacheCN 学习资源汇总 2019.3
查看>>
每隔1s打印0-5
查看>>
企业云服务器的选择与配置指南
查看>>
Python中eval与exec的使用及区别
查看>>
如何利用es6去重
查看>>
小心,querySelector前方10米有坑
查看>>
刘奇:我们最喜欢听用户说的话是「你们搞得定吗?」 | TiDB DevCon 2019
查看>>
缓存在高并发场景下的常见问题
查看>>
组合视图
查看>>
用Js实现给一个1-100的数字,让计算机用最少的次数猜中这个数字
查看>>
解决高德地图label添加的点击事件在移动端无效
查看>>
css实现盒尺寸重置、均匀分布的子元素、截断文本
查看>>
docker
查看>>
js 定时器笔记
查看>>
JavaScript文本特效实例小结
查看>>
(git应用)一台电脑多项目版本控制
查看>>
【跃迁之路】【637天】程序员高效学习方法论探索系列(实验阶段394-2018.11.10)...
查看>>