• + 0 comments

    Python 3 Just filter for happy conditions

    from collections import Counter
    def happyLadybugs(b):
        # Write your code here
        if len(b) == 1 and b[0] == '_':
            return 'YES'
        if len(b) == 1 and b[0] != '_':
            return 'NO'
        c = Counter(b)
        flag = 0
        if c['_'] == 0:
            for i in range(len(b)):
                if i == 0:
                    if b[i] == b[i+1]:
                        continue
                    else:
                        flag+=1
                if i == len(b)-1:
                    if b[i] == b[i-1]:
                        continue
                    else:
                        flag+=1
                if i > 0 and i < len(b)-1:
                    if b[i] == b[i-1] or b[i] == b[i+1]:
                        continue
                    else:
                        flag+=1
                        
        for m, n in c.items():
            if n <= 1 and m != '_':
                return 'NO'
                
        return 'YES' if not flag else 'NO'