Maximum Length Substring With Two Occurrences

Given a string `s`, return the maximum length of a substring in which every character appears at most two times.

30:00

Maximum Length Substring With Two Occurrences
easy
Free written solution
Topics
Companies

Given a string s, return the maximum length of a substring in which every character appears at most two times.

Example 1:
Input: {"s":"bcbbbcba"}
Output: 4
Constraints:
  • 2≤s.length≤1002 \leq \text{s.length} \leq 100

  • s consists only of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"bcbbbcba"}

Every letter gets two pegs. Grow the window to the right; if a letter would need a third peg, shrink from the left until it fits again.

s with the window

b

L

c

L

b

L

b

L

b

L

c

L

b

L

a

L

peg board: two pegs per letter

a

b

c

window = empty

best = 0

Variables
VariableValue
left0
best0
DepthFunction Call
Stack empty
0/20