原题链接在这里:https://leetcode.com/problems/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 characters.

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

T is "ece" which its length is 3.

题解:

类似Longest Substring with At Most Two Distinct Characters.

2变成k即可.

Time Complexity: O(n), n = s.length(). Space: O(1), map size.

AC Java:

 1 public class Solution {
 2     public int lengthOfLongestSubstringKDistinct(String s, int k) {
 3         int res = 0;
 4         int [] map = new int[256];
 5         int walker = 0;
 6         int runner = 0;
 7         int count = 0;
 8         while(runner < s.length()){
 9             if(map[s.charAt(runner++)]++ == 0){
10                 count++;
11             }
12             while(count > k){
13                 if(map[s.charAt(walker++)]-- == 1){
14                     count--;
15                 }
16             }
17             res = Math.max(res, runner - walker);
18         }
19         return res;
20     }
21 }

跟上Longest Repeating Character Replacement.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/6384945.html

LeetCode 340. Longest Substring with At Most K Distinct Characters相关推荐

  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 two distinct char ...

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

  3. LeetCode 159. Longest Substring with At Most Two Distinct Characters --Java,C++,Python解法

    题目地址:Longest Substring with At Most Two Distinct Characters - LeetCode Given a string s , find the l ...

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

  5. leetcode 395. Longest Substring with At Least K Repeating Characters| 395. 至少有 K 个重复字符的最长子串(分治法)

    题目 https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ 题解 参考:官方题解 ...

  6. 386 · Longest Substring with At Most K Distinct Characters最多有k个不同字符的最长子字符串

    链接:https://www.lintcode.com/problem/386/description https://mp.weixin.qq.com/s?__biz=MzU2OTUyNzk1NQ= ...

  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 Longest Substring with At Most Two Distinct Characters 滑动窗口法

    题目解析 代码如下 题目解析 这一题是一道会员的题目,题目介绍如下: Given a string, find the length of the longest substring T that c ...

  9. 【Lintcode】1375. Substring With At Least K Distinct Characters

    题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...

最新文章

  1. 基于FPGA的AM信号调制与解调详细步骤
  2. CTF-Crypto密码学
  3. 原生DOM选择器querySelector和querySelectorAll
  4. 50.本地VMware环境虚拟机的异地(Azure)容灾(上)
  5. android创建桌面快捷方式
  6. Baklib知识库-企业知识库管理平台
  7. Pug 介绍和在 Vue 中使用
  8. DBeaver 连接 人大金仓以及其他国产数据库
  9. python单词表首字母排序_python 字母排序不区分大小写
  10. PPT计算机原理结构初步,测量实践初步(赖丽娟).ppt
  11. 一文详解BQSR-碱基质量矫正原理和实战
  12. 关于使用idea输入中文时,候选框不出现在光标附近的问题
  13. 第二次项目《吃货联盟Plus》
  14. Flask后端实践 连载十三 Flask输出Excel报表
  15. Lazada(来赞达):就三个步骤!搞定Lazada店铺装修
  16. 用Google实现站内搜索
  17. 好用的免费代理池搭建
  18. 【深度学习】步态识别-论文阅读:(T-PAMI-2021)综述:Deep Gait Recognition
  19. 徐州好玩实用的微信小程序
  20. NIO 选择 Onsemi 的 SiC 牵引功率模块

热门文章

  1. Sql Server 分区演练
  2. 研究了 2 天,终于知道 JDK 8 默认 GC 收集器了!
  3. 历史上最简单的一道Java面试题,但无人能通过
  4. Visual Studio Code高效开发----自动保存设置方法
  5. 网络:窗口控制下的重发机制、流量控制
  6. JVM:类加载机制之类加载过程
  7. 剑指offer:约瑟夫环的问题
  8. 【Linux - mysql】linux下mysql报Failed to restart mysqld.service: Unit not found
  9. 数据结构-求一个字符串中的某个子串(C语言)
  10. element ui 中el-input搜索输入框或者普通输入框无法输入的问题讨论