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.
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相关推荐
- [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 ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- 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= ...
- [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 ...
- 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 ...
- [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 ...
- 【Lintcode】1375. Substring With At Least K Distinct Characters
题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...
- leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法
题目解析 代码如下 题目解析 这一题是一道会员的题目,题目介绍如下: Given a string, find the length of the longest substring T that c ...
- 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 ...
最新文章
- 掌握 React 与 React Native
- 前端学习(2737):重读vue电商网站47之生成打包报告
- java 歌词_请问吧内有大神用JAVA做过桌面歌词吗
- linux安装mysql5.6整套_Linux安装Mysql5.6
- html带取消的谈窗框,HTML参考
- python 交集_Python设置交集
- 判断显卡好坏的步骤、新买显卡装机后不亮解决
- 使用SqlBulkCopy类实现导入excel表格
- ISO27001信息安全管理体系
- 智能识别系统设计---图像特征提取
- 移动端touch事件影响界面click/超链接事件无法点击
- 计算机中丢失cg,【计算机中丢失dll文件】计算机丢失dll文件_计算机中丢失cg dll-win7之家...
- 【数据库】MySQL 加锁处理分析
- H5 开发内嵌页面跨域问题
- 人人都是产品经理读书笔记
- CSP-S 2022游记
- 如何用Git将代码上传到GitHub
- 华为无线设备配置静态负载均衡
- 139邮箱无法连接服务器,手机号登录邮箱,为什么总是连接不到服务器?
- html中outline属性,css 轮廓(outline)属性是如何使用的
热门文章
- .Net Core下基于Emit的打造AOP
- 【codeforces 749E】 Inversions After Shuffle
- Dubbo xml配置 和注解配置 写法
- Android - 基于Toolbar的Navigation Drawer(Material Design)
- 一款纯css3实现的超炫动画背画特效
- 某final神犇的工作感想
- UVa11137 Ingenuous Cubrency
- 在Matlab2006a中如何创建.net组件
- 使用matlab工具研究神经网络的简单过程(网络和数据下载)
- springboot : Failed to decode downloaded font 和 OTS parsing error