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. 最简单的TCP、UDP案例及各函数的详细解释

    TCP: server #include "stdafx.h" #include<iostream> #define BUF_SZIE 64 #include &quo ...

  2. mysql 笔记分享

    mysql LPAD 和RPAD不足位数补齐填充函数总结一下mysql数据库的一些特征MySQL WHERE 语句优化之我见mysql limit 实例详解mysql 如何实现多表联合更新MySQL ...

  3. python带参数装饰器使用

    # -*- coding: utf-8 -* """TensorFlow指定使用GPU工具类 author: Jill usage: 方法上加@tf_with_devic ...

  4. PyQt5系列教程

    PyQt5系列教程(一)Mac OS X下搭建Python3.5.1+PyQt5开发环境PyQt5系列教程(二)利用QtDesigner设计UI界面PyQt5系列教程(三)用py2exe进行程序打包P ...

  5. 逆地址解析协议RARP

    解决的问题 一般系统启动时,从引导磁盘中获取ip 有些机器没有引导磁盘,如X终端或无盘工作站,则需要采用其他方法来获得IP地址 解决的过程 无盘系统依据RARP协议 从接口卡上读取唯一的硬件地址,然后 ...

  6. api 1.1构架篇

    首先让其自动加载??? 在YiiBase.php里面 改写autoload方法:     /**     * Class autoload loader.     * This method is p ...

  7. Oracle JOB简例

    JOB declare jobno number; begin dbms_job.submit( jobno, 'insert into tmptable1 values(1,1);', to_dat ...

  8. 1. SVN clean失败解决方法

    svn执行clean up后出现提示:svn cleanup failed–previous operation has not finished; run cleanup if it was int ...

  9. Linux 配置TomCat 项目三大步骤

    一:  安装 JRE 01: 下载 server-jre 安装包 => http://www.oracle.com/technetwork/java/javase/downloads/serve ...

  10. HTML5 Canvas ( 图形变换, 升级版的星空 ) translate, rotate, scale

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...