1156. Swap For Longest Repeated Character Substring LeetCode Solution

In this guide, you will get 1156. Swap For Longest Repeated Character Substring LeetCode Solution with the best time and space complexity. The solution to Swap For Longest Repeated Character Substring problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Swap For Longest Repeated Character Substring solution in C++
  4. Swap For Longest Repeated Character Substring solution in Java
  5. Swap For Longest Repeated Character Substring solution in Python
  6. Additional Resources
1156. Swap For Longest Repeated Character Substring LeetCode Solution image

Problem Statement of Swap For Longest Repeated Character Substring

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
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.

Constraints:

1 <= text.length <= 2 * 104
text consist of lowercase English characters only.

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1156. Swap For Longest Repeated Character Substring LeetCode Solution in C++

class Solution {
 public:
  int maxRepOpt1(string text) {
    int ans = 0;
    vector<int> count(26);
    vector<pair<char, int>> groups{{text[0], 1}};

    for (const char c : text)
      ++count[c - 'a'];

    for (int i = 1; i < text.length(); ++i)
      if (text[i] == text[i - 1])
        ++groups[groups.size() - 1].second;
      else
        groups.emplace_back(text[i], 1);

    for (const auto& [c, length] : groups)
      ans = max(ans, min(length + 1, count[c - 'a']));

    for (int i = 1; i + 1 < groups.size(); ++i)
      if (groups[i - 1].first == groups[i + 1].first && groups[i].second == 1)
        ans = max(ans, min(groups[i - 1].second + groups[i + 1].second + 1,
                           count[groups[i - 1].first - 'a']));

    return ans;
  }
};
/* code provided by PROGIEZ */

1156. Swap For Longest Repeated Character Substring LeetCode Solution in Java

class Solution {
  public int maxRepOpt1(String text) {
    int ans = 0;
    int[] count = new int[26];
    List<int[]> groups = new ArrayList<>();

    for (final char c : text.toCharArray())
      ++count[c - 'a'];

    groups.add(new int[] {text.charAt(0), 1});

    for (int i = 1; i < text.length(); ++i)
      if (text.charAt(i) == text.charAt(i - 1))
        ++groups.get(groups.size() - 1)[1];
      else
        groups.add(new int[] {text.charAt(i), 1});

    for (int[] group : groups)
      ans = Math.max(ans, Math.min(group[1] + 1, count[group[0] - 'a']));

    for (int i = 1; i + 1 < groups.size(); ++i)
      if (groups.get(i - 1)[0] == groups.get(i + 1)[0] && groups.get(i)[1] == 1)
        ans = Math.max(ans, Math.min(groups.get(i - 1)[1] + groups.get(i + 1)[1] + 1,
                                     count[groups.get(i - 1)[0] - 'a']));

    return ans;
  }
}
// code provided by PROGIEZ

1156. Swap For Longest Repeated Character Substring LeetCode Solution in Python

class Solution:
  def maxRepOpt1(self, text: str) -> int:
    count = collections.Counter(text)
    groups = [[c, len(list(group))]
              for c, group in itertools.groupby(text)]
    ans = max(min(length + 1, count[c]) for c, length in groups)

    for i in range(1, len(groups) - 1):
      if groups[i - 1][0] == groups[i + 1][0] and groups[i][1] == 1:
        ans = max(
            ans,
            min(groups[i - 1][1] + groups[i + 1][1] + 1, count
                [groups[i - 1][0]]))

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.