EasyHash TableStringSliding WindowUpdated Sep 2026

3090. Maximum Length Substring With Two Occurrences

Asked at Meta

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Hash Table

Companies

1

Problem

You are given a string s made of lowercase English letters. Find the length of the longest substring in which no character appears more than twice.

A substring is a contiguous block of characters — you must take characters that sit next to each other, with no gaps. You cannot skip characters.

Return a single integer: the length of the longest valid substring. If several substrings tie for the longest, any of them gives the same answer.

Find the longest block with no character used 3 times

slength 4bcbbbcba01234567Inside this block b appears twice and c once, so it is valid.sb appears 3 timesbcbbbcba01234567Stretching the block by one more character gives b three times,which is not allowed.
The block "bcbb" is valid because b appears twice and c once; adding the next character would break the rule.

Constraints

  • ●2 <= s.length <= 100
  • ●s consists only of lowercase English letters

Examples

Example 1
Inputs = "bcbbbcba"
Output4
WhyThe substring "bcbb" (positions 1 to 4) has b twice and c once, so every character appears at most twice. Its length is 4, and no longer block satisfies the rule.
Example 2
Inputs = "aaaa"
Output2
WhyEvery character is 'a'. A block of length 3 would contain 'a' three times, which is not allowed, so the best we can do is length 2.

Asked At

1 companies
CompanyDifficulty
MetaEasyView all Meta questions →

How to Think About It

1

Brute force: enumerate all substrings, check if each character appears at most twice, and track the maximum length.

2

Use a sliding window with a hash map to count character frequencies in the current window.

3

Expand the right pointer and increment the count for s[right].

4

When any character count exceeds 2, shrink the window from the left until the condition is satisfied again.

5

At each valid window, update the maximum length found so far.

Optimal Approach

Use a variable-size sliding window with a frequency map. Expand the right pointer and increment counts. When any character appears more than twice, shrink the left pointer until the constraint is restored. Track the maximum window size throughout. This runs in O(n) time since each character is visited at most twice (once by right, once by left).

Sliding window with a frequency count

swindow = 4bcbbbcba01234567leftrightStep 1 — the window b,c,b,b is valid: b twice, c once.swindow = 4bcbbbcba01234567leftrightStep 2 — right adds a third b, so left moves past the first b andthe window is valid again.count[ch] += 1 when right moveswhile count[s[right]] > 2: drop s[left], left += 1best = max(best, right - left + 1)
The right pointer grows the window; when a letter hits three copies, the left pointer moves until the window is valid again.
ComplexityTime O(n)Space O(1)Each character enters the window once and leaves at most once, and the frequency map holds at most 26 letters.

What Trips People Up in Real Interviews

1

Clarify the constraint: is it "at most two occurrences" or "exactly two occurrences" — the problem says at most.

2

Edge case: empty string — return 0.

3

Edge case: all characters are the same, e.g., "aaaa" — the answer is min(len, 2).

4

This is a direct application of the variable-size sliding window pattern.

5

Contrast with "at most K distinct characters" problems — here the constraint is per-character frequency, not distinct count.

Solution Code

O(n) time
def maximumLengthSubstring(s):
    from collections import defaultdict
    count = defaultdict(int)
    left = 0
    max_len = 0
    for right in range(len(s)):
        count[s[right]] += 1
        while count[s[right]] > 2:
            count[s[left]] -= 1
            left += 1
        max_len = max(max_len, right - left + 1)
    return max_len

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Maximum Length Substring With Two Occurrences problem?

Given a string s, return the length of the longest substring where each character appears at most twice. A substring is a contiguous sequence of characters within the string.

How do you solve Maximum Length Substring With Two Occurrences?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Maximum Length Substring With Two Occurrences?

Maximum Length Substring With Two Occurrences is asked at Meta. It is a easy difficulty problem.

What are common mistakes on Maximum Length Substring With Two Occurrences?
  • Clarify the constraint: is it "at most two occurrences" or "exactly two occurrences" — the problem says at most.
  • Edge case: empty string — return 0.
  • Edge case: all characters are the same, e.g., "aaaa" — the answer is min(len, 2).
  • This is a direct application of the variable-size sliding window pattern.
  • Contrast with "at most K distinct characters" problems — here the constraint is per-character frequency, not distinct count.