Given a string, find the longest substring that contains only two unique characters. For example, given "abcbbbbcccbdddadacb", the longest substring that contains k unique character is "bcbbbbcccb".

分析:

用hashmap记录每个character从start到当前位置出现的次数,如果第k + 1个character出现, 更新maxLength,我们需要把左边的pointer(start) 往右移,直到从start到current之间只有K个character.

 public class Solution {
public int lengthOfLongestSubstringKDistinct(String s, int k) {
if (s == null) return ;
if (s.length() <= k) return s.length(); int begin = , end = ;
Map<Character, Integer> map = new HashMap<>();
int tempMaxLength = Integer.MIN_VALUE; while (end < s.length()) {
map.put(s.charAt(end), map.getOrDefault(s.charAt(end), ) + );
while (map.size() > k) {
if (map.get(s.charAt(begin)) == ) {
map.remove(s.charAt(begin));
} else {
map.put(s.charAt(begin), map.get(s.charAt(begin)) - );
}
begin++;
}
tempMaxLength = Math.max(tempMaxLength, end - begin + );
end++;
}
return tempMaxLength;
}
}

Longest Substring with At Most K Distinct Characters的更多相关文章

  1. [LeetCode] 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 "Longest Substring with At Most K Distinct Characters"

    A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...

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

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

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

  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: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结

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

  7. [Swift]LeetCode340.最多有K个不同字符的最长子串 $ Longest Substring with At Most K Distinct Characters

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

  8. 最多有k个不同字符的最长子字符串 · Longest Substring with at Most k Distinct Characters(没提交)

    [抄题]: 给定一个字符串,找到最多有k个不同字符的最长子字符串.eg:eceba, k = 3, return eceb [暴力解法]: 时间分析: 空间分析: [思维问题]: 怎么想到两根指针的: ...

  9. LeetCode340 Longest Substring with At Most K Distinct Characters

    This is a question needs pay for , I have no money to pay ,so just write some test case by myself. I ...

随机推荐

  1. Linux进程管理工具——supervisor

    介绍 Supervisord是用Python实现的一款非常实用的进程管理工具 安装 这里用源码 supervisor-.tar.gz .tar.gz cd supervisor- sudo pytho ...

  2. Eclipse属性文件编辑器---Properties Editor

    今天在用 Eclipse 来编辑 .properties 文件时,写的中文会自动转为 Unicode 编码,完全不知道自己的中文写的是什么!! 于是查了一下,网上推荐,在Eclipse 中 安装一个 ...

  3. Java基础-数据类型int,short,char,long,float,double,boolean,byte

    Java语言是静态类型的(statical typed),也就是说所有变量和表达式的类型再编译时就已经完全确定.由于是statical typed,导致Java语言也是强类型(Strong typed ...

  4. 【Matplotlib】 标注细节注意

    相关文档: Artists BBox 由于蓝线和红线的存在,现在刻度标注很难看清楚.我们可以使他们更大,也可以使它们的属性以便使得线呈现半透明的白色背景.这样做我们既可以看到数据也可以看到刻度标注了. ...

  5. Date类型时间转换

    /* 时间转换start */ public static void main(String args[]) { Date nowTime = new Date(); System.out.print ...

  6. Java编程思想学习(一) 一切都是对象

    前言 Java是基于C++的,但Java是一种更加纯粹的面向对象程序设计语言. C++和Java都是混合/杂合型语言.杂合型语言允许多种编程风格. 用引用操纵对象 每种编程语言都有自己操纵内存中元素的 ...

  7. BZOJ-1192 鬼谷子的钱袋 2^n有关数论

    1192: [HNOI2006]鬼谷子的钱袋 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2473 Solved: 1806 [Submit][St ...

  8. COGS8 备用交换机

    备用交换机 ★★   输入文件:gd.in   输出文件:gd.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] n个城市之间有通讯网络,每个城市都有通讯交换机,直接或间 ...

  9. TCP/IP详解 学习六

    从ip层看路由表  选路策略 选路策略:决定把哪些路由放到路由表的规则. Ip执行选路机制,而路由守护程序则提供选路策略. Netstat –rn 打印路由表,如果没有-n命令会搜索配置文件,将网络地 ...

  10. android.net.wifi的简单使用方法

    获取Wifi的控制类WifiManager.  WifiManager  wm=(WifiManager)getSystemService(Context.WIFI_SERVICE); 接下来可以对w ...