• + 0 comments

    Python3; After realizing 1 empty field gives you possibility to place any existing color anywhere

    from collections import Counter
    
    def are_happy(b):
        for i in range(len(b)):
            happy = False
            if i > 0:
                if b[i-1] == b[i]:
                    happy = True
            if i < len(b) - 1:
                if b[i+1] == b[i]:
                    happy = True
            if not happy:
                return False
        return True
            
    
    def happyLadybugs(b):
        c = Counter(b)
        if c['_'] < 1:
            if are_happy(b):
                return 'YES'
            else:
                return 'NO'
        
        for key, count in c.most_common()[::-1]:
            if count == 1 and key != '_':
                return 'NO'
            if count > 1:
                return 'YES'
        return 'YES'