[LeetCode]Longest Substring with At Most Two Distinct Characters

news/2024/7/3 17:58:41

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.

分析

这种类型的题都应该用两个指针解决,同时用一个map来记录字符及其出现次数。一个右指针先移动,不断更新map, 当发现map里的字符个数大于规定个数的时候,开始移动左指针,同时更新map,直到map里的字符个数等于规定个数,中间不断更新包含规定字符个数的最大长度。

这道题的Follow up很直接,比如题目变为最多允许k个字符,该怎么处理?

下面的代码直接把2改成k即可。

另外,跟这道题方法类似,但相对复杂些的类似题可参考Minimum Window Substring。

复杂度

time: O(n), space: O(n)

代码

public class Solution {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
        Map<Character, Integer> map = new HashMap<>();
        int left = 0;
        int i = 0;
        int maxLen = 0;
        while (i < s.length()) {
            
            // 根据右指针指的当前字符更新map
            char c = s.charAt(i);
            if (!map.containsKey(c)) {
                map.put(c, 1);
            } else {
                map.put(c, map.get(c) + 1);
            }
            
            // 移动左指针,直到map中字符数量降至规定数量
            while (map.size() > 2) {
                char leftChar = s.charAt(left);
                if (map.containsKey(leftChar)) {
                
                    // 注意会有重复元素,所以先减小次数,只有次数降至0,才删除元素
                    map.put(leftChar, map.get(leftChar) - 1);                     
                    if (map.get(leftChar) == 0) { 
                        map.remove(leftChar);
                    }
                }
                left++;
            }               
            maxLen = Math.max(maxLen, i - left + 1);
            i++;
        }
        return maxLen;
    }
}

http://www.niftyadmin.cn/n/4030316.html

相关文章

【JOURNAL】2.14纪事

-- 去南京图书馆办理中外文借阅证&#xff0c;押金四百&#xff0c;并随手借书两册&#xff1a;论意大利最古老的智慧 H771.3/01 What is personality? B84/533 另外&#xff1a;南京图书馆的闭架书竟然不外借&#xff01;难道我对“闭架”的理解一直有误&#xff1f;再另外&a…

近期团队博客的摘要 1

Windows Essential Business Server简介 这篇文章介绍我们开发EBS的目的&#xff0c;产品的主要功能&#xff0c;以及我们能解决的中型企业IT所面临的一系列问题。 点击这里 阅读原文。 Silverlight中的Downloader对象&#xff08;javascript&#xff09; Silverlight中国研发…

mysql报错1872: Slave failed to initialize relay log info structure from the repository

ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository在一台主机上增加一个slave&#xff0c;启动的时候报[ERROR] Slave SQL: Slave failed to initialize relay log info structure from the repository, Error_code: 1872原因&…

初识 Ansible

一、Ansible的介绍运维工具常见的工作模式 12agent模式: 基于ssl实现。代理工作在被监控端。像puppet。 agentless模式: 基于ssh服务实现工作在被监控端。监控端是ssh的客户端。ansible是工作在agentless模式下具有幂等性。ansible在控制端只需要告诉监控端的期望状态就可以实…

咏梅

咏梅 古往今来&#xff0c;梅花&#xff0c;被无数文人骚客咏叹&#xff0c;无不是触景生情&#xff0c;假物言志&#xff0c;最为熟悉的有陆游、李商隐、李清照等的咏梅诗词让人们千古传唱&#xff0c;以宋词为最。 陆游的卜算子.咏梅我们都耳熟能详&#xff1a; 驿外断桥边&a…

读书笔记- programing .net components(o'Reilly)

好久没来写博了&#xff0c;最近开始读一本新书&#xff0c;在这标记下。刚刚读了没几天&#xff0c;给我的感觉就是这本书很对我的胃口&#xff0c;但由于本人英文水平有限&#xff0c;理解起来有些费劲。 转载于:https://www.cnblogs.com/lazhgg/archive/2008/02/27/1084185.…

Windows 摄像头数据

1、 FFmpeg获取DirectShow设备数据&#xff08;摄像头&#xff0c;录屏&#xff09; http://blog.csdn.net/leixiaohua1020/article/details/38284961 2、 转载于:https://www.cnblogs.com/CodeSkill/p/5085598.html

学用 TStringGrid [4] - ColWidths、RowHeights

本例功能:1、调整单元宽度;2、调整单元高度.运行效果图://本例代码: unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls, Grids;typeTForm1 class(TForm)StringGrid1: TStringGrid;Panel1: TP…