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
Constraints
- ●
2 <= s.length <= 100 - ●
s consists only of lowercase English letters
Examples
s = "bcbbbcba"4s = "aaaa"2Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Meta | Easy | View all Meta questions → |
How to Think About It
Brute force: enumerate all substrings, check if each character appears at most twice, and track the maximum length.
Use a sliding window with a hash map to count character frequencies in the current window.
Expand the right pointer and increment the count for s[right].
When any character count exceeds 2, shrink the window from the left until the condition is satisfied again.
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
What Trips People Up in Real Interviews
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.
Solution Code
O(n) timedef 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_lenFrequently 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.