Maximum Length Substring With Two Occurrences

Given a string s, return the maximum length of a substring such that it contains at most two occurrences of each character.

Example 1
Inputs = "bcbbbcba"
Output4
A substring of length 4 can be chosen such that each character appears at most twice, and no longer valid substring exists.
Example 2
Inputs = "aaaa"
Output2
The longest substring with at most two occurrences of each character is "aa", which has length 2.

Constraints

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

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate