Swap For Longest Repeated Character Substring

You are given a string text. You can swap two of the characters in text.

Return the length of the longest substring with repeated characters.

Example 1
Inputtext = "ababa"
Output3
We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a', making the longest repeated character substring "aaa" with length 3.
Example 2
Inputtext = "aaabaaa"
Output6
Swap 'b' with the last 'a' (or the first 'a'), and the longest repeated character substring is "aaaaaa" with length 6.

Constraints

  • 1 <= text.length <= 2 * 10^4
  • text consist of lowercase English characters only.

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