• + 0 comments
    def lenIfValidResult(s, charA, charB):
        last_matching_char = None
        count = 0
        for char in s:
            if char == charA or char == charB:
                if last_matching_char == char:
                    return 0
                count += 1
                last_matching_char = char
        return count
    
    def alternate(s):    
        max_len = 0
        chars_in_str = set(s)
        if len(chars_in_str) <= 1:
            return 0
        for charA in chars_in_str:
            for charB in chars_in_str:
                if charA != charB:
                    result_len = lenIfValidResult(s, charA, charB)
                    max_len = max(max_len, result_len)
        return max_len