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.

Analyses: Map each character in the string into a index in an array times, size of that array is from ASCII size. During the process of scanning, update the frequencies of each character. Have a variable count to label how many different characters occur. Whenever the count exceeds k, we know that shorten our range: Let the left pointer move right until the frequency of the character pointed by left is 1.

 1 public class Solution {
 2     public int lengthOfLongestSubstringKDistinct(String s, int k) {
 3         int result = 0, left = 0, n = s.length(), count = 0;
 4         int[] times = new int[256]; // map char in s to a index
 5         for (int right = 0; right < n; right++) {
 6             if (times[s.charAt(right)]++ == 0) count++;
 7
 8             if (count > k) {
 9                 while (--times[s.charAt(left++)] > 0);
10                 count--;
11             }
12
13             result = Math.max(result, right - left + 1);
14         }
15         return result;
16     }
17 }

转载于:https://www.cnblogs.com/amazingzoe/p/6768022.html

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 340. Longest Substring with At Most K Distinct Characters

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

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

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

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

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

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

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

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

  9. 395. Longest Substring with At Least K Repeating Characters

    题目要求 Find the length of the longest substring T of a given string (consists of lowercase letters onl ...

最新文章

  1. 掌握 React 与 React Native
  2. 前端学习(2737):重读vue电商网站47之生成打包报告
  3. java 歌词_请问吧内有大神用JAVA做过桌面歌词吗
  4. linux安装mysql5.6整套_Linux安装Mysql5.6
  5. html带取消的谈窗框,HTML参考
  6. python 交集_Python设置交集
  7. 判断显卡好坏的步骤、新买显卡装机后不亮解决
  8. 使用SqlBulkCopy类实现导入excel表格
  9. ISO27001信息安全管理体系
  10. 智能识别系统设计---图像特征提取
  11. 移动端touch事件影响界面click/超链接事件无法点击
  12. 计算机中丢失cg,【计算机中丢失dll文件】计算机丢失dll文件_计算机中丢失cg dll-win7之家...
  13. 【数据库】MySQL 加锁处理分析
  14. H5 开发内嵌页面跨域问题
  15. 人人都是产品经理读书笔记
  16. CSP-S 2022游记
  17. 如何用Git将代码上传到GitHub
  18. 华为无线设备配置静态负载均衡
  19. 139邮箱无法连接服务器,手机号登录邮箱,为什么总是连接不到服务器?
  20. html中outline属性,css 轮廓(outline)属性是如何使用的

热门文章

  1. .Net Core下基于Emit的打造AOP
  2. 【codeforces 749E】 Inversions After Shuffle
  3. Dubbo xml配置 和注解配置 写法
  4. Android - 基于Toolbar的Navigation Drawer(Material Design)
  5. 一款纯css3实现的超炫动画背画特效
  6. 某final神犇的工作感想
  7. UVa11137 Ingenuous Cubrency
  8. 在Matlab2006a中如何创建.net组件
  9. 使用matlab工具研究神经网络的简单过程(网络和数据下载)
  10. springboot : Failed to decode downloaded font 和 OTS parsing error