Maximum Length Substring With Two Occurrences - Problem
Maximum Length Substring With Two Occurrences

You're given a string s, and your task is to find the maximum length of any substring where each character appears at most twice.

Think of it as finding the longest "balanced" portion of text where no character becomes too repetitive. For example, in the string "abcaba", the entire string is valid since each character ('a', 'b', 'c') appears at most 2 times, giving us a maximum length of 6.

Goal: Return the length of the longest valid substring
Input: A string s containing lowercase English letters
Output: An integer representing the maximum substring length

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "bcbbbcba"
โ€บ Output: 4
๐Ÿ’ก Note: The optimal substring is "bcbb" with frequencies: b=3 (invalid), c=1. Actually, "cbbb" won't work either. The correct answer is "bcbb" -> "cbb" (length 3) or "bcb" (length 3) or "bbc" (length 3) or "cbbc" (length 4) where c=2, b=2. So maximum length is 4.
example_2.py โ€” All Characters Valid
$ Input: s = "abcaba"
โ€บ Output: 6
๐Ÿ’ก Note: The entire string is valid since character frequencies are: a=3 (invalid). Wait, let me recalculate: a appears at positions 0,3,5 (3 times), b appears at positions 1,4 (2 times), c appears at position 2 (1 time). Since 'a' appears 3 times, we need a substring. The longest valid substring is "bcab" or "caba" or "abca" (length 4) where no character appears more than twice.
example_3.py โ€” Single Character
$ Input: s = "aa"
โ€บ Output: 2
๐Ÿ’ก Note: The string "aa" has character frequency: a=2, which satisfies the constraint of at most 2 occurrences per character.

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of lowercase English letters only
  • Each character can appear at most 2 times in a valid substring

Visualization

Tap to expand
Sliding Window: Maximum Valid SubstringString: "bcbbbcba"bcbbbcba01234567Window [1,4]: "cbbb"c: 1, b: 3 โŒ (b appears 3 times)Invalid - need to shrink windowWindow [2,5]: "bbbc"b: 3, c: 1 โŒ (b appears 3 times)Still invalid - continue shrinkingWindow [3,6]: "bbcb"b: 3, c: 1 โŒ (b appears 3 times)Still invalidWindow [4,7]: "bcba"b: 2, c: 1, a: 1 โœ“Valid! Length = 4Maximum Length Found: 4Optimal substring: "bcba" where each character appears โ‰ค 2 timesAlgorithm Complexity:โฑ๏ธ Time: O(n) - each char visited โ‰ค 2 times๐Ÿง  Space: O(1) - at most 26 characters tracked๐ŸŽฏ Technique: Sliding Window + Hash Map
Understanding the Visualization
1
Initialize Window
Start with an empty window at the beginning of the string
2
Expand Right
Keep adding characters to the right side of the window
3
Monitor Frequencies
Track how many times each character appears in the current window
4
Shrink When Invalid
When any character appears 3+ times, remove characters from the left
5
Track Maximum
Remember the largest valid window size encountered
Key Takeaway
๐ŸŽฏ Key Insight: The sliding window technique efficiently maintains a valid substring by expanding right and contracting left as needed, ensuring each character appears at most twice while tracking the maximum valid length.
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
23.5K Views
Medium Frequency
~15 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen