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: t is "ece" which its length is 3.

Example 2:

Input: "ccaabbb"
Output: 5
Explanation: t is "aabbb" which its length is 5.

题意:

给定字符串,找出至多包含两种字符的最长子串。

思路:

j          
 “e c e b a”
   i                    e-0
     i                  c-1
         i              e-2
-------map.size()<= 2
            i           b-3  此时移动 j

代码:

 class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
if(s.length() < 2) return s.length();
HashMap<Character, Integer> map = new HashMap<>();
int result = 0;
int j = 0;
for(int i = 0; i < s.length(); ){
char c = s.charAt(i);
if(map.size() <= 2){
map.put(c, i);
i++;
}
if(map.size() > 2){
int leftMost = s.length();
for(int n : map.values()){
leftMost = Math.min(leftMost, n);
}
map.remove(s.charAt(leftMost));
j = leftMost + 1;
}
result = Math.max(result, i - j );
}
return result;
}
}

[leetcode]159. Longest Substring with At Most Two Distinct Characters至多包含两种字符的最长子串的更多相关文章

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

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

  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

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

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

  6. 【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 ...

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

  8. LeetCode 340. Longest Substring with At Most K Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...

  9. 【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)

    Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...

随机推荐

  1. [UE4]ChildActor组件

    ChildActor组件可以让一个actor成为另外actor的组成部分,并在视图中展示出来.

  2. django (装饰器,母版继承,自定义,request对象,response对象)

     1. 装饰器  1.    def wrapper(fn):    def inner(*args,**kwargs):     执行被装饰函数之前的操作     ret = fn(*args,** ...

  3. jieba库及wordcloud库的使用

    知识内容: 1.jieba库的使用 2.wordcloud库的使用 参考资料: https://github.com/fxsjy/jieba https://blog.csdn.net/fontthr ...

  4. centos6.5 64练手安装memcached,PHP调试

    思路  先安装 memcached  然后安装php的基于扩展libmemcache ,然后安装php memcache扩展包,然后把扩展添加到php.ini 1 yum安装 简单方便 yum ins ...

  5. Latex Error:‘acmart.cls’ not found 解决方案:

    windows下latex编译ACM论文模板时,出现Latex Error:‘acmart.cls’ not found,解决方案:  首先cd至模板所在目录下,然后运行以下命令:  tex acma ...

  6. VBA 编写类

    一.初识类 现在,请打开你的VBE,主菜单-插入-类模块. 插入了一个类模块,也就建立了一个类.类模块的名字就是类的名字.你现在看到的,她的名字叫“类1”,这是VBA按她姐妹排行给她取的的,是的,VB ...

  7. UI5-文档-4.16-Dialogs and Fragments

    在这一步中,我们将进一步研究另一个可以用来组装视图的元素:the fragment. 片段是轻量级UI部件(UI子树),可以重用,但是没有任何控制器.这意味着,每当你想定义一个特定UI的一部分是跨多个 ...

  8. Haskell语言学习笔记(55)Data.Vector

    Data.Vector Construction Prelude V> import Data.Vector as V Prelude V> V.empty [] Prelude V> ...

  9. Python使用Threading模块创建线程

    使用Threading模块创建线程,直接从threading.Thread继承,然后重写__init__方法和run方法: #!/usr/bin/python # -*- coding: UTF-8 ...

  10. pyplot绘图区域

    pyplot绘图区域 Matplotlib图像组成 matplotlib中,整个图像为一个Figure对象,与用户交互的整个窗口 Figure对象中包含一个或多个Axes(ax)子对象,每个ax子对象 ...