↓Skip to main content
  1. LeetCode/

LeetCode 1156: Swap For Longest Repeated Character Substring

·2 mins· ·
LeetCode Medium Sliding-Window Hash-Table String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-23

  • Submissions: Accepted (first try)

Approach
#

With at most one swap, the final run is some character c. Handle each c separately:

  • Find the longest window with at most one character that is not c; that character can be swapped with a c elsewhere.
  • But the swapped-in c must come from outside the window. If every c is already inside, none can come in, and the best is to move an edge c inward, giving the total count of c.
  • So this c contributes min(window length, total[c]).

At most 26 characters, each with one O(n) sliding window.

My Solution
#

class Solution:
    def maxRepOpt1(self, text: str) -> int:
        total = Counter(text)
        best = 0
        for c in total:
            left = other = 0
            for right, ch in enumerate(text):
                other += ch != c
                while other > 1:                   # at most one non-c
                    other -= text[left] != c
                    left += 1
                best = max(best, min(right - left + 1, total[c]))
        return best

Complexity
#

Time O(26 · n), space O(26).

Mistakes to Avoid
#

  • Forgetting the cap at the total count of c: "aaabaaa" has only 6 as, so the answer is 6, not 7.

Interview Explanation
#

The final run is some character c, so for each character I find the longest window with at most one non-c, which can be swapped for a c elsewhere. That c has to come from outside, so the length is capped at the total count of c, and I take the smaller. One window per character: O(26n).

Related

LeetCode 3306: Count of Substrings Containing Every Vowel and K Consonants II
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 3306 note: “exactly k consonants” is not monotonic; compute “all five vowels and at least k consonants” minus “at least k + 1”, both “at least” counting windows
LeetCode 1358: Number of Substrings Containing All Three Characters
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 1358 note: an “at least one each of a, b, c” counting window: keep shrinking while all three are present, then count += left
LeetCode 3325: Count Substrings With K-Frequency Characters I
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 3325 note: an “at least one character appears k times” counting window; when the new character reaches k, keep shrinking until it drops to k - 1
LeetCode 395: Longest Substring with At Least K Repeating Characters
·3 mins
LeetCode Medium Sliding-Window Divide-and-Conquer Hash-Table String
LeetCode 395 note: “every character at least k times” is not monotonic, so a direct slide fails; divide and conquer splits on characters with fewer than k occurrences, or fixing the number of distinct characters makes the window slidable
LeetCode 2516: Take K of Each Character From Left and Right
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 2516 note: taking from both ends leaves a middle block in which each letter may appear at most total − k times; find the longest such window
LeetCode 1016: Binary String With Substrings Representing 1 To N
·3 mins
LeetCode Medium Sliding-Window String Bit-Manipulation Hash-Table
LeetCode 1016 note: s has at most 1000 characters, so there are few substrings of any fixed length and a large n must be False; checking from n downward fails fast, or extend up to 30 bits from each start to collect every value <= n