input: from collections import Counter
  
  def max_length_substring(s: str) -> int:
      """
      Given a string s, determine the maximum length of a 
      substring which contains no more than two instances of each character.
  
      Examples:
      >>> s = "bcbbbcba"
      4
      >>> s = "aaaa"
      2
  
      Constraints:
      2 <= s.length <= 100
      s consists only of lowercase English letters.
      """
output:     def is_valid(counter):
          return all(value <= 2 for value in counter.values())
  
      n = len(s)
      max_len = 0
      left = 0
      counter = Counter()
  
      for right in range(n):
          counter[s[right]] += 1
  
          while not is_valid(counter):
              counter[s[left]] -= 1
              if counter[s[left]] == 0:
                  del counter[s[left]]
              left += 1
          
          max_len = max(max_len, right - left + 1)
  
      return max_len