Practice Maximum Length Substring With Two Occurrences
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Maximum Length Substring With Two Occurrences
Problem Statement
Find the greatest length of a contiguous substring of s in which no character appears more than twice. Return the length, not the substring. A character appearing once or not at all is allowed. Practice approach: Maintain a sliding window and character frequencies. After adding the next character, move the left boundary until that character's count is at most two again, decrementing counts for removed characters. All other counts were already valid. Save the largest valid window. Each position enters and leaves once, giving O(n) time and constant space for the lowercase alphabet.
Source: Maximum Length Substring With Two Occurrences on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
s = "bcbbbcba"
4
# Example 2
s = "aaaa"
2
Constraints
Related LeetCode Problems
Practice more problems similar to Maximum Length Substring With Two Occurrences.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.