Swap For Longest Repeated Character Substring - Problem
Given a string text, you can perform at most one swap of any two characters in the string. Your goal is to maximize the length of the longest substring containing repeated characters.

A substring is a contiguous sequence of characters within the string. After the optional swap, you want to find the longest possible substring where all characters are identical.

Example: In string "ababa", swapping positions 1 and 2 gives "aabba", creating a substring "aa" of length 2.

Note: You don't have to perform a swap if the original string already contains the optimal solution.

Input & Output

example_1.py โ€” Basic case
$ Input: "ababa"
โ€บ Output: 3
๐Ÿ’ก Note: We can swap positions 0 and 2 to get "abaab", then positions 2 and 4 to get "aabab". But optimally, swap positions 1 and 3 to get "aabaa", giving us a substring "aaa" of length 3.
example_2.py โ€” All same characters
$ Input: "aaaa"
โ€บ Output: 4
๐Ÿ’ก Note: The string already consists of repeated characters, so no swap is needed. The entire string has length 4.
example_3.py โ€” Two character types
$ Input: "aaabbaaa"
โ€บ Output: 6
๐Ÿ’ก Note: We can swap one 'b' with an 'a' to create a longer sequence of 'a's. For example, swap position 3 (first 'b') with position 4 (second 'b') doesn't help, but we can rearrange to get "aaaabaaa" giving us 4 consecutive 'a's, or "aaaaaaabb" giving us 6 consecutive 'a's by moving characters optimally.

Constraints

  • 1 โ‰ค text.length โ‰ค 105
  • text consists of lowercase English letters only
  • You can perform at most one swap operation

Visualization

Tap to expand
Swap for Longest Repeated Character SubstringExample: "abcaa" โ†’ Goal: Maximize repeated charactersOriginal String:abcaaCurrent max: 2 ("aa")Strategy: Sliding window for character 'a' (appears 3 times)Window can contain at most 2 different charsabcaaCan achieve length 5!Possible result after optimal swap:aaaaaLength: 5 โœ“All same character!๐ŸŽฏ Key Insight:Instead of trying all swaps, we use sliding window to find the longestachievable sequence for each character type in O(n) time!
Understanding the Visualization
1
Analyze Original
Count each character type and find current longest repeated substring
2
For Each Character
Use sliding window to find longest achievable sequence for each character type
3
Simulate Swap
Allow at most one 'wrong' character in window that could be swapped out
4
Find Maximum
Return the maximum achievable length across all character types
Key Takeaway
๐ŸŽฏ Key Insight: Rather than actually performing swaps, we can simulate their effect using a sliding window that allows at most one character that could be 'swapped out' to achieve our target character sequence.
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 25
52.4K Views
High Frequency
~18 min Avg. Time
1.8K 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