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. CSS重点巩固

    一:position定位 a: static 定位 ,HTML元素的默认值,即没有定位,元素出现在正常的流中.静态定位的元素不会受到top, bottom, left, right影响. b: fix ...

  2. psd做成HTML相关参考页面

    前端制作(美工)是怎么把PSD制作成页面的? 美工怎么做的我不清楚,因为我是做前端的,我就从前端这个角度说吧. 首先拿到PSD,先分析哪些是要导出为图片的,哪些是可以自己用代码完成的.将图片全部导出, ...

  3. openvpn的介绍和搭建过程

    本文摘自:http://www.linuxidc.com/Linux/2012-01/51702.htm,在这只是为了做个笔记使用

  4. 网站性能工具Yslow的使用方法

    Yslow是雅虎开发的基于网页性能分析浏览器插件,从年初我使用了YSlow后,改变了博客模板大量冗余代码,不仅提升了网页的打开速度,这款插件还帮助我分析了不少其他网站的代码,之前我还特意写了提高网站速 ...

  5. VS插件之小番茄

    文件源以及安装说明! http://www.youranshare.com/app/98.html

  6. hdu4825 字典树 XOR

    用字典树思想来做.对于一个数,给出他的二进制,然后更具二进制建立字典树,然后每次询问的时候的数也给出二进制,如果当前为1,那就向0走,为0,向1走. #include<stdio.h> # ...

  7. jquery插件库

    jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多javascript高手加入其team. jQuery是继prototype之后又一个优秀的Javascrīpt框架.其经典 ...

  8. 和声搜索算法-python实现

    HSIndividual.py import numpy as np import ObjFunction class HSIndividual: ''' individual of harmony ...

  9. 以一个权限系统来告别WebForm —开篇

     前言: 当今是互联网的时代,我们己经阻止不了它的发展了,只有跟上脚步,才不会被抛弃,松散了这么久,该紧紧了.  背景: 我之所以说以一个权限应用系统来告别我的WebForm内部系统的生涯,是缘于我自 ...

  10. ajax入门详解

    l 一个实例 在开始正式讲解 Ajax之前,首先让我们先来看看Google Map使用Ajax改善其产品设计的效果. 1. 在浏览器地址栏中输入http://maps.google.com打开Goog ...