Longest Substring with At Most Two Distinct Characters

2024-09-04 15:38

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

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

For example,Given s = “eceba”,

T is "ece" which its length is 3.

思路:同向双指针,跟Longest Substring with At Most K Distinct Characters 一模一样,i是主指针,j是辅指针,然后模板套起来,注意要判断如果即将要大于k的时候,j停下来,不做任何事情,update res,然后i++;代码跟K一模一样,只是把k改成2即可;

class Solution {public int lengthOfLongestSubstringTwoDistinct(String s) {if(s == null || s.length() == 0) {return 0;}char[] ss = s.toCharArray();int[] scount = new int[256];int C = 0;int j = 0;int maxlen = 0;for(int i = 0; i < ss.length; i++) {// move j;while(j < ss.length && C <= 2) {if(scount[ss[j]] == 0) {if(C == 2) {break;}C++;}scount[ss[j]]++;j++;}// update result;// 最多是k,小于k也必须update maxlen;if(j - i > maxlen) {maxlen = j - i;}// remove i;scount[ss[i]]--;if(scount[ss[i]] == 0) {C--;}}return maxlen;}
}

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



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

相关文章

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

详解MySQL中DISTINCT去重的核心注意事项

《详解MySQL中DISTINCT去重的核心注意事项》为了实现查询不重复的数据,MySQL提供了DISTINCT关键字,它的主要作用就是对数据表中一个或多个字段重复的数据进行过滤,只返回其中的一条数据... 目录DISTINCT 六大注意事项1. 作用范围:所有 SELECT 字段2. NULL 值的特殊处

解决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(

Java多线程编程模式实战指南:Two-phase Termination模式

文章来源: http://www.infoq.com/cn/articles/java-multithreaded-programming-mode-two-phase-termination?utm_source=infoq&utm_campaign=user_page&utm_medium=link 文章代码地址: https://github.com/Visce

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资源商店链接资源