Algorithm


Problem Name: 1156. 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.

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public ListNode swapPairs(ListNode head) {
    ListNode curr = head;
    ListNode prev = null;
    while (curr != null && curr.next != null) {
      ListNode nextNode = curr.next;
      if (curr == head) {
        head = nextNode;
      }
      curr.next = nextNode.next;
      nextNode.next = curr;
      if (prev != null) {
        prev.next = nextNode;
      }
      prev = curr;
      curr = curr.next;
    }
    return head;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
text = "ababa"

Output

x
+
cmd
3

#2 Code Example with Javascript Programming

Code - Javascript Programming


const maxRepOpt1 = function(text) {
  const count = [...text].reduce((a, c) => {
    a[c] = a[c] || 0;
    a[c]++;
    return a;
  }, {});
  let ans = 0;
  let i = 0;
  while (i  <  text.length) {
    let j = i;
    const c = text.charAt(i);
    while (j  <  text.length && text.charAt(j) === c) j++;
    if (j - i < count[c]) {
      let k = j + 1;
      while (k  <  text.length && text.charAt(k) === c && k - i < count[c]) k++;
      ans = Math.max(k - i, ans);
    } else ans = Math.max(j - i, ans);
    i = j;
  }
  return ans;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
text = "ababa"

Output

x
+
cmd
3

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def maxRepOpt1(self, S: str) -> int:
        A = [[c, len(list(g))] for c, g in itertools.groupby(S)]
        count = collections.Counter(S)
        res = max(min(k + 1, count[c]) for c, k in A)
        for i in range(1, len(A) - 1):
            if A[i - 1][0] == A[i + 1][0] and A[i][1] == 1:
                res = max(res, min(A[i - 1][1] + A[i + 1][1] + 1, count[A[i + 1][0]]))
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
text = "aaabaaa"

Output

x
+
cmd
6
Advertisements

Demonstration


Previous
#1155 Leetcode Number of Dice Rolls With Target Sum Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1157 Leetcode Online Majority Element In Subarray Solution in C, C++, Java, JavaScript, Python, C# Leetcode