Swap For Longest Repeated Character Substring
Medium · Hash Table, String, Sliding Window
You are given a string text. You can swap two of the characters in the text.
Return the length of the longest substring with repeated characters.
Example 1
Input: text = "ababa" Output: 3
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
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
No need to swap, longest repeated character substring is "aaaaa" with length is 5.
Constraints
- 1 <= text.length <= 2 * 104
- text consist of lowercase English characters only.
Starter code (python)
class Solution(object):
def maxRepOpt1(self, text):
"""
:type text: str
:rtype: int
"""
Related problems
- Two Sum (Easy)
- Longest Substring Without Repeating Characters (Medium)
- Longest Palindromic Substring (Medium)
Previous: Day of the Year · Next: Number of Dice Rolls With Target Sum · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.