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. iOS边练边学--cocoaPods管理第三方框架--命令行方式实现

    更换源 Gem是一个管理Ruby库和程序的标准包,它通过Ruby Gem(如 http://rubygems.org/)源来查找.安装.升级和写在软件包 gem sources --remove ht ...

  2. Java-Vector

    package 集合类.list类; import java.util.Vector; public class Vector类 { public static void main(String[] ...

  3. 获取和设置tinyMCE 4编辑器的内容

    对于tinymce编辑器是无法通过js进行内容的读写的,必须使用编辑器自身的方法才行,下面是一些方法,希望能对用到的朋友有所帮助: 1.如果当前页面只有一个编辑器: 获取内容:tinyMCE.acti ...

  4. ASP.NET MVC3 局部页面@RENDERBODY @RENDERPAGE@RENDERSECTION使用方法详细说明

    转载自:http://blog.163.com/wenchangqing_live/blog/static/173722309201211299817278/ asp.net mvc3局部页面使用方法 ...

  5. BZOJ-2190 仪仗队 数论+欧拉函数(线性筛)

    今天zky学长讲数论,上午水,舒爽的不行..后来下午直接while(true){懵逼:}死循全程懵逼....(可怕)Thinking Bear. 2190: [SDOI2008]仪仗队 Time Li ...

  6. BZOJ-1822 Frozen Nova 冷冻波 计(jie)算(xi)几何+二分+最大流判定+经典建图

    这道逼题!感受到了数学对我的深深恶意(#‵′).... 1822: [JSOI2010]Frozen Nova 冷冻波 Time Limit: 10 Sec Memory Limit: 64 MB S ...

  7. BZOJ4590 自动刷题机

    Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题机刷题的方式非常简单:首先会瞬间得出题目的正确做法,然后开始写 ...

  8. POJ2253 Frogger

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34865   Accepted: 11192 Descrip ...

  9. Xcon2014 && Geekpwn2014

    目录 . 链接器与加载器技术在保护壳上的应用 . android应用市场中的大规模漏洞挖掘 . android模拟躲避检测和应对 . 内核链表的奥秘 . 信号的可发现性 -- wifi之外我们还能做什 ...

  10. 数组添加:如何往数组的"null"位置插入数据呢?

    数组添加,当已经存在的一个数组时,如何往数组的"null"位置插入数据呢? 分析: 1.循环遍历数组元素,找出null的位置(下标) 2.设置一个变量,接收null位置下标值 3. ...