• + 0 comments

    from collections import Counter

    def happyLadybugs(b):

    count_b = Counter(b)
    mc = count_b.most_common()
    if len(mc)!=1 and ((mc[-1][1]== 1 and mc[-1][0]!= '_') or mc[-2][1]== 1):
        return 'NO'
    else:
        if '_' in b:
            return 'YES'
        for i in mc:
            if count_b[i[0]]==1 or b.rfind(i[0])-b.find(i[0])+1 != count_b[i[0]]:
                return 'NO'
            if i == mc[-1]:
                return 'YES'