Skip to content

3090. Maximum Length Substring With Two Occurrences Easy

Given a string s, return the maximum length of a substring such that it contains at most two occurrences of each character.

Example 1:
Input: s = "bcbbbcba"
Output: 4
Explanation:
The following substring has a length of 4 and contains at most two occurrences of each character: "bcbbbcba".

Example 2:
Input: s = "aaaa"
Output: 2
Explanation:
The following substring has a length of 2 and contains at most two occurrences of each character: "aaaa".

Approach

Input: A string s

Output: Return the maximum length of a substring consisting of at most two occurrences of each unique character

This problem falls under the Variable-length Sliding Window category.

We use two pointers (left and right) to maintain a sliding window while taking advantage of a hash map to log the frequency mapping values reflecting appearance occurrences uniquely characterizing variables trapped within bounds sequentially. While mapping sequences expanding our active frame moving the forward index pointer right, any element values logged hitting values larger than 2 instantly triggers constraints demanding left-bound compressions uniformly tightening dimensions backwards sliding incrementally resolving structural bounds satisfying < 2 values stably globally maintaining correct sequences continuously linearly perfectly.

As frame scaling passes legality parameter testing consistently dynamically mapping longest substring evaluations checking properties concurrently determining eventual valid results natively sequentially accurately logically terminating optimal outcomes yielded optimally.

Implementation

python
class Solution:
    def maximumLengthSubstring(self, s: str) -> int:
        freq_map = {}  # Tracks element mapping values characteristically spanning internal structural sequence intervals continuously
        left = 0       # Sliding windows lowest boundary frame constraints parameter logic
        max_len = 0    # Top value results dynamically scaling max length records

        for right in range(len(s)):
            char = s[right]
            freq_map[char] = freq_map.get(char, 0) + 1  # Populate current variable frequencies

            # Compress trailing left boundary limits effectively mapping illegal counts > 2 properly adjusting ranges  
            while freq_map[char] > 2:
                freq_map[s[left]] -= 1
                left += 1

            # Determine best lengths mapping continuous intervals optimizing constraints properly evaluated dynamically
            max_len = max(max_len, right - left + 1)

        return max_len  # Outputs maximal boundary size conforming directly matching metrics natively
javascript
var maximumLengthSubstring = function(s) {
    const freq = {};

    let left = 0;
    let ans = 0;
    for (let right = 0; right < s.length; right++) {
        const ch = s[right];

        freq[ch] = (freq[ch] || 0) + 1;

        while (freq[ch] > 2) {
            freq[s[left]]--;
            left++;
        }

        ans = Math.max(ans, right - left + 1);
    }

    return ans;
};

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1) space size is restricted logically exclusively since the maximum distinct string letters equates to size strictly corresponding alphabet components capped logically mapping 26 spaces

3090. Maximum Length Substring With Two Occurrences (English)3090. 每个字符最多出现两次的最长子字符串 (Chinese)