#1156
Swap For Longest Repeated Character Substring
specialist · 960 · lc medium +32 · verified · 44.2% accepted · 1,082 likes · top 26%
Description
Given a string text, you may perform at most one swap of any two characters anywhere in the string. After the optional swap, return the length of the longest contiguous substring in which every character is identical.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa" with length 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa" with length 6.
Example 3:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa" with length is 5.
Code
1
2
3