Talentd
FleetCode

3090. Maximum Length Substring With Two Occurrences

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

Example 1:

Input: s = "bcbbbcba"

Output: 4

Explanation:

The following substring has a length of 4 and contains at most two occurrences of each character: "bcbbbcba".

Example 2:

Input: s = "aaaa"

Output: 2

Explanation:

The following substring has a length of 2 and contains at most two occurrences of each character: "aaaa".

Constraints:

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

Asked at 2 companies

Hints open one at a time

Hint 1
We can try all substrings by brute-force since the constraints are very small.
7:05
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maximumLengthSubstring(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"bcbbbcba"
Input
"aaaa"
Your code is saved on this device.Solution