↓Skip to main content
  1. LeetCode/

LeetCode 3090: Maximum Length Substring With Two Occurrences

·1 min· ·
LeetCode Easy Sliding-Window Hash-Table String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-01-10

  • Submissions: Accepted (first try)

Approach
#

A window is valid ⇔ every character appears at most twice. Each time the right boundary adds a character, only that character can exceed the limit, so shrink the left until it is back to two.

LeetCode 2958 is the same problem with k in place of 2.

My Solution
#

class Solution:
    def maximumLengthSubstring(self, s: str) -> int:
        cnt = Counter()
        left = best = 0
        for right, c in enumerate(s):
            cnt[c] += 1
            while cnt[c] > 2:                      # only the new c can exceed
                cnt[s[left]] -= 1
                left += 1
            best = max(best, right - left + 1)
        return best

Complexity
#

Time O(n), space O(1) (at most 26 characters).

Mistakes to Avoid
#

  • Checking the whole count table every time: only the newly added character can break the rule.

Interview Explanation
#

A longest-window slide where each character appears at most twice. After adding a character on the right, only it can exceed two, so I shrink the left until it is back to two and track the longest length. Time O(n).

Related

LeetCode 1016: Binary String With Substrings Representing 1 To N
·3 mins
LeetCode Medium Sliding-Window String Bit-Manipulation Hash-Table
LeetCode 1016 note: s has at most 1000 characters, so there are few substrings of any fixed length and a large n must be False; checking from n downward fails fast, or extend up to 30 bits from each start to collect every value <= n
LeetCode 2269: Find the K-Beauty of a Number
·2 mins
LeetCode Easy Sliding-Window Math String
LeetCode 2269 note: convert the number to a string and check every length-k substring; 0 cannot be a divisor
LeetCode 2953: Count Complete Substrings
·3 mins
LeetCode Hard Sliding-Window Hash-Table String
LeetCode 2953 note: split where neighboring letters differ by more than 2; in each segment enumerate the number of distinct letters d, slide a fixed window of d × k, and track how many letters appear exactly k times
LeetCode 3545: Minimum Deletions for At Most K Distinct Characters
·1 min
LeetCode Easy Greedy Hash-Table String Sorting Counting
LeetCode 3545 note: reducing the number of kinds means deleting whole kinds, starting from the least frequent
LeetCode 3541: Find Most Frequent Vowel and Consonant
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 3541 note: count characters, take the maximum among vowels and among consonants and add them; a missing class counts as 0
LeetCode 3120: Count the Number of Special Characters I
·1 min
LeetCode Easy Hash-Table String
LeetCode 3120 note: collect the letters seen in lowercase and in uppercase; the size of their intersection is the answer