Longest Substring with At Most K Distinct Characters

2024-09-04 15:38

本文主要是介绍Longest Substring with At Most K Distinct Characters,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

T is "ece" which its length is 3.

思路:跟  Longest Substring with at Most 2 Characters.思路一模一样。i是主指针,j是辅指针,然后模板套起来,注意要判断如果即将要大于k的时候,j停下来,不做任何事情,update res,然后i++;

class Solution {public int lengthOfLongestSubstringKDistinct(String s, int k) {if(s == null || s.length() == 0) {return 0;}int curcount = 0;int count = 0;int j = 0;int res = 0;int[] scount = new int[256];for(int i = 0; i < s.length(); i++) {// move j;while(j < s.length() && curcount <= k) {if(curcount == k) {if(scount[s.charAt(j)] == 0) {break;}}if(scount[s.charAt(j)] == 0) {curcount++;}scount[s.charAt(j)]++;j++;}// update res;res = Math.max(res, j - i);// move i;scount[s.charAt(i)]--;if(scount[s.charAt(i)] == 0) {curcount--;}}return res;}
}

这篇关于Longest Substring with At Most K Distinct Characters的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136364

相关文章

解决The valid characters are defined in RFC 7230 and RFC 3986

解决方法: 一、更换低版本的Tomcat;(我选的方案) 二、参考:https://blog.csdn.net/qq_32365919/article/details/82055800

(nyoj308)substring

Substring 时间限制: 1000 ms  |  内存限制: 65535 KB 难度: 1 描述 You are given a string input. You are to find the longest substring of input such that the reversal of the substring is also a substring of

leetcode#32. Longest Valid Parentheses

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", wh

【python 编码问题】UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not

插入oracle 数据发生 错误:UnicodeEncodeError: 'ascii' codec can't encode characters in position 131-136: ordinal not in range(128) 先说解决办法: python2.7版本,在开头加入下面语句 import sysreload(sys)sys.setdefaultencoding

MySQL中的`SUBSTRING()`和`MID()`函数:精准抽取字符串中的子串

在数据库操作中,经常需要从存储的字符串中提取出特定的部分,比如从用户全名中提取姓氏、从日期字符串中提取年份等。MySQL提供了SUBSTRING()和MID()两个函数,它们的功能几乎完全相同,都是用来从字符串中抽取子串的。本文将详细介绍这两个函数的用法、参数以及在实际场景中的应用。 一、SUBSTRING()和MID()函数的基本语法 1. SUBSTRING()函数 SUBSTRING(

mysql 的函数用法SUBSTRING_INDEX

因为数据库的数据要更新操作,内容是这样的: 这是之前的数据,现在因为需求变更,只需要横杠之前的数据,数据量少可以手动改,但是有几百条的数据,所以找到了一个方法 UPDATE product SET pro_price=SUBSTRING_INDEX(pro_price, '-', 1);  这个SUBSTRING_INDEX就是用来截取的 pro_price是要修改的字段名,然后中间

HYPERCASUAL - Simple Characters(卡通游戏火柴人物模型)

介绍HyperCasual - 简单角色! 一套低多边形角色资源,用于创建超休闲风格的游戏。 包含演示场景 角色(x10) 生化人、小丑、Flaty_Boss、女孩、守门员、英雄、亚马逊女战士、男人、红衣男人、修理工 每个网格大约有700-2000个顶点 角色设置与Mecanim兼容(本包中不包含动画) 着色器适用于可编写脚本的渲染管线(HD + LW) 下载:​​Unity资源商店链接资源

java常用算法之最长回文子串(Longest Palindromic Substring)

方法一:时间复杂度为O(n^3) public static String longestPalindrome1(String s) {int maxPalinLength = 0;String longestPalindrome = null;int length = s.length();// check all possible sub stringsfor (int i = 0; i

[LeetCode] 300. Longest Increasing Subsequence

题:https://leetcode.com/problems/longest-increasing-subsequence/description/ 题目 Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7

[LeetCode] 409. Longest Palindrome

题:https://leetcode.com/problems/longest-palindrome/description/ 题目 Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with