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

For example, Given s = “eceba” and k = 2,

T is "ece" which its length is 3.

题意:

给定字符串,求至多包含K种字符的最长子串

思路:

[leetcode]159. Longest Substring with At Most Two Distinct Characters至多包含两种字符的最长子串思路大体相同

  1. j
  2. j
  3. S= e c e b a and k = 2, return 3 for "e c e"
  4. i e-0 map.size <=2 move i
  5. i c-1 map.size <=2 move i
  6. i e-2(update) map.size <=2 move i
  7. --------- b-3 map.size >2 get the length then move j
  8. i

代码:

  1. class Solution {
  2. public int lengthOfLongestSubstringKDistinct(String s, int k) {
  3. //corner
  4. if(s.length() < k ) return s.length();
  5. // general
  6. HashMap<Character, Integer> map = new HashMap<>();
  7. int j = 0;
  8. int result = 0;
  9. for(int i = 0; i < s.length(); ){
  10. char c = s.charAt(i);
  11. if(map.size() <= k){
  12. map.put(c, i);
  13. i++;
  14. }
  15. if(map.size() > k){
  16. int leftMost = s.length();
  17. for(int n : map.values()){
  18. leftMost = Math.min(n, leftMost);
  19. }
  20.  
  21. map.remove(s.charAt(leftMost));
  22. j = leftMost + 1;
  23. }
  24. result = Math.max(i - j , result);
  25.  
  26. }
  27. return result;
  28.  
  29. }
  30. }

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

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

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

  4. ✡ 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 ...

  5. leetcode[159] Longest Substring with At Most Two Distinct Characters

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

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

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

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

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

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

随机推荐

  1. JQUERY dialog的用法详细解析

    本篇文章主要是对JQUERY中dialog的用法进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助 今天用到了客户端的对话框,把 jQuery UI 中的对话框学习了一下. 准备 jQ ...

  2. div+Css绝对定位(absolute)和相对定位(relative)的总结

    1.没有外Div的情况下 设置绝对定位(absolute)和相对定位(relative)是没有区别的 2.相对定位占位置 而绝对定位不占位置 会漂浮在别的Div之上 3.若父Div没有设置定位,子Di ...

  3. Deep Reinforcement Learning 基础知识

    Introduction 深度增强学习Deep Reinforcement Learning是将深度学习与增强学习结合起来从而实现从Perception感知到Action动作的端对端学习的一种全新的算 ...

  4. solr6.3根据搜索关键词词频(关键词出现次数、关键词highlight)进行排序

    http://localhost:8080/solr/test/select?fq=product_name:大有&indent=on&q=product_name:大有电钻 OR r ...

  5. Python - Django - ORM 操作数据

    查询数据(查询管理员): app01/models.py 中定义的类,也就是创建的表 from django.db import models # 类必须继承 models.Model class A ...

  6. Executor框架(五)Executors工厂类

    Executors 简介 Executors 是一个工厂类,其提供的是Executor.ExecutorService.ScheduledExecutorService.ThreadFactory 和 ...

  7. Executor框架(六)CompletionService 接口

      CompletionService 接口是一个独立的接口,并没有扩展 ExecutorService . 其默认实现类是ExecutorCompletionService;   接口 Comple ...

  8. 24. (ora-01410无效的rowid)临时表 on commit delete rows 与 on commit preserve rows 的区别

    ora-01410无效的rowid解决方式: 把临时表空间改成会话级别的就可以了,即把临时表的创建选项由on commit delete rows改为on commit preserve rows,就 ...

  9. 6、SpringMVC源码分析(1):分析DispatcherServlet.doDispatch方法,了解总体流程

    所有的http请求都会交给DispatcherServlet类的doDispatch方法进行处理,将DispatcherServlet.doDispatch函数的javadoc复制到下面: /* * ...

  10. JS 报表制作

    1:Echarts, 界面多样化. http://echarts.baidu.com/index.html 2: Jmgraph 工具 简单的画线工具 http://graph.jm47.com/ 3 ...