• + 28 comments

    The solution to this certainly doesn't need regex. And I'm pretty sure that basic usage of sets (just finding unique entries in a string) counts as an easy problem.

    The question is phrased to intentionally lead you to a difficult solution, so figuring out what the easy solution is may make it not an 'easy question'.

    Edit: Lots of downvotes on this comment I see... Since I just pointed out that only basic usage of a set is needed to solve this reasonably (and certainly not any regex), and I agreed that it may not be an 'easy' question, I'm a bit confused...

    def validate(cpy):
        for i in range(len(cpy)-1):
            if cpy[i] == cpy[i+1]:
                return False
        return True
    
    for _ in range(int(input().strip())):
        s = input().strip()
        st = list(set(s))
        max_len = 0
        for x in range(len(st)):
            for y in range(x+1, len(st)):
                cpy = [c for c in s if c==st[x] or c==st[y]]
                if validate(cpy):
                    max_len = max(max_len, len(cpy))
        print(max_len)