Longest Substring with At Most Two of Each Character
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
# Longest Substring with At Most Two of Each Character
Given a string `s`, return the maximum length of a contiguous substring in which no character occurs more than twice.
The limit applies separately to each character. A valid substring may contain more than two distinct characters.
### Examples
**Example 1**
```text
Input: s = "abcabcbb"
Output: 6
```
The first six characters, `"abcabc"`, contain two occurrences each of `a`, `b`, and `c`. The two length-seven substrings each contain at least three occurrences of `b`, so neither they nor the whole string are valid.
**Example 2**
```text
Input: s = "abaccca"
Output: 5
```
The substring `"abacc"` is valid and has length five. Both length-six substrings contain three occurrences of `c`, and the whole string is also invalid, so a longer valid substring does not exist.
### Constraints
- `2 <= s.length <= 100`.
- `s` contains only lowercase English letters.
### Clarifying Questions
- Is this a subsequence problem? No; the chosen characters must be contiguous.
- Does every present character have to occur exactly twice? No; one or two occurrences are allowed.
- Is there a limit of two distinct characters? No; each distinct character has its own two-occurrence limit.
- Is the substring itself returned? No; return only its length.
### C++ Interface
```cpp
class Solution {
public:
int maximumLengthSubstring(string s) {
}
};
```
### Reasoning Checkpoints
- Apply the occurrence limit independently to every character in a contiguous interval.
- Establish that the returned length is achievable and no longer valid interval exists.
- Cover repeated characters and the input-length boundaries, and justify resource use.
```hint Enumerate starts and extend the end
The input has at most 100 characters. For each starting position, extend a substring while maintaining character counts. Once one count exceeds two, further extension from that same start cannot make it valid again.
```
Overview: Find the longest contiguous substring with at most two occurrences of every character, including strings with many distinct letters.
Longest Substring with At Most Two of Each Character
Meta
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0
Longest Substring with At Most Two of Each Character
Given a string s, return the maximum length of a contiguous substring in which no character occurs more than twice.
The limit applies separately to each character. A valid substring may contain more than two distinct characters.
Examples
Example 1
Input: s = "abcabcbb"
Output: 6
The first six characters, "abcabc", contain two occurrences each of a, b, and c. The two length-seven substrings each contain at least three occurrences of b, so neither they nor the whole string are valid.
Example 2
Input: s = "abaccca"
Output: 5
The substring "abacc" is valid and has length five. Both length-six substrings contain three occurrences of c, and the whole string is also invalid, so a longer valid substring does not exist.
Constraints
2 <= s.length <= 100
.
s
contains only lowercase English letters.
Clarifying Questions Guidance
Is this a subsequence problem? No; the chosen characters must be contiguous.
Does every present character have to occur exactly twice? No; one or two occurrences are allowed.
Is there a limit of two distinct characters? No; each distinct character has its own two-occurrence limit.
Is the substring itself returned? No; return only its length.
C++ Interface
class Solution {
public:
int maximumLengthSubstring(string s) {
}
};
Reasoning Checkpoints
Apply the occurrence limit independently to every character in a contiguous interval.
Establish that the returned length is achievable and no longer valid interval exists.
Cover repeated characters and the input-length boundaries, and justify resource use.