• + 0 comments

    for Python3 Platform

    import re
    import itertools
    
    def alternate(s):
        maxLen = 0
        unique_char = set(s)
        n = len(unique_char)
        pattern = re.compile(r'^(\w)(?!\1)(\w)(\1\2)*\1?$')
        
        if (n < 2):
            maxLen = 0
        else:
            for comb in itertools.combinations(unique_char, n-2):
                x = s
                
                for i in comb:
                    x = x.replace(i, "")
                
                if (pattern.match(x)):
                    maxLen = max(maxLen, len(x))
        
        return maxLen
    
    length = int(input())
    s = input()
    
    result = alternate(s)
    
    print(result)