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就行了,具体讲解请参考之前那篇博客:

解法一:

class Solution {
public:int lengthOfLongestSubstringKDistinct(string s, int k) {int res = 0, left = 0;unordered_map<char, int> m;for (int i = 0; i < s.size(); ++i) {++m[s[i]];while (m.size() > k) {if (--m[s[left]] == 0) m.erase(s[left]);++left;}res = max(res, i - left + 1);}return res;}
};

具体讲解请参考之前那篇博客Longest Substring with At Most Two Distinct Characters,参见代码如下:

解法二:

class Solution {
public:int lengthOfLongestSubstringKDistinct(string s, int k) {int res = 0, left = 0;unordered_map<char, int> m;for (int i = 0; i < s.size(); ++i) {m[s[i]] = i;while (m.size() > k) {if (m[s[left]] == left) m.erase(s[left]);++left;}res = max(res, i - left + 1);}return res;}
};

类似题目:

Longest Substring with At Most Two Distinct Characters

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串...相关推荐

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

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

  3. leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法

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

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

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

  6. 【算法】159题 Longest Substring with at Most Two Distinct Characters 最大的子串

    1.概述 给定一个字符串.查找一个最长子序列,这个子序列中重复的字符最多包含2个. 此问题和 [算法]剑指 Offer 48. 最长不含重复字符的子字符串 问题很相似. 2.方案 2.1 双指针 pu ...

  7. Leetcode每日必刷题库第3题,如何实现无重复字符的最长子串?

    题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: 示例 1: 输入: "abcabcbb" 输出: 3  解释: 因为无重复字符的最长子串是 &qu ...

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

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

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

最新文章

  1. 赠书 | 手把手教你自己动手打造一个智能恒温器
  2. 小型自动化运维--expect脚本之自动同步
  3. Hadoop应用实战100讲(二)-Hadoop常用命令汇总
  4. android引用另一个项目作为library错误
  5. oracle导入报错39002,oracle impdp ORA-39002
  6. 一起学习C语言:数组(二)
  7. 学习云计算学哪种编程语言_您应该学习哪种编程语言?
  8. python中split拆分数组_Python 数组分割
  9. Spring Boot ES 实战,直接拿来用!
  10. 精进 Quartz—Quartz大致介绍(一)
  11. 算法第四版 高清完整中文版(非纸质版)
  12. 2000~2022年Java学习笔记
  13. 分享两款雪花特效代码
  14. Android Update Engine 分析(十七)10 类 InstallOperation 数据的生成和应用
  15. poj1125 Stockbroker Grapevine Floyd算法
  16. 解决ios下的微信页面背景音乐无法自动播放问题
  17. 信息安全系统设计基础期中总结
  18. 无盘服务器网线接法,无盘经验:解析网吧千兆网线的标准做法
  19. win10快捷方式变成空白图标怎么办?
  20. WP Smush Pro网站图片优化插件最新版免费下载

热门文章

  1. IE6,7,8,FF兼容总结
  2. 防止IIS文件被下载方法
  3. 死锁问题分析的利器——valgrind的DRD和Helgrind
  4. Google Test(GTest)使用方法和源码解析——预处理技术分析和应用
  5. Python3中参数*args和**kwargs介绍
  6. Ubuntu14.04 64位上配置终端显示git分支名称
  7. linux哪个版本支持多线程,关于Linux操作系统的叙述错误的是()A、Linux是多用户、多任务、支持多线程的操作系统B、Linux的源...
  8. qt发布后 mysql数据库_qt发布后 mysql数据库
  9. php多线程查表,php curl_multi 多线程查询的例子
  10. 哪个工具编译java_用什么工具来编译JAVA啊?