java.lang.Object
g1101_1200.s1156_swap_for_longest_repeated_character_substring.Solution

public class Solution extends Object
1156 - Swap For Longest Repeated Character Substring.<p>Medium</p> <p>You are given a string <code>text</code>. You can swap two of the characters in the <code>text</code>.</p> <p>Return <em>the length of the longest substring with repeated characters</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> text = &ldquo;ababa&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> We can swap the first &lsquo;b&rsquo; with the last &lsquo;a&rsquo;, or the last &lsquo;b&rsquo; with the first &lsquo;a&rsquo;. Then, the longest repeated character substring is &ldquo;aaa&rdquo; with length 3.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> text = &ldquo;aaabaaa&rdquo;</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> Swap &lsquo;b&rsquo; with the last &lsquo;a&rsquo; (or the first &lsquo;a&rsquo;), and we get longest repeated character substring &ldquo;aaaaaa&rdquo; with length 6.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> text = &ldquo;aaaaa&rdquo;</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> No need to swap, longest repeated character substring is &ldquo;aaaaa&rdquo; with length is 5.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= text.length <= 2 * 10<sup>4</sup></code></li> <li><code>text</code> consist of lowercase English characters only.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxRepOpt1

      public int maxRepOpt1(String text)