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

相关文章

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

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] 583. Delete Operation for Two Strings

题:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目 Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in