• + 1 comment

    To address this problem, I utilized a dictionary-based approach. There are two distinct conditions to consider:

    1. Under the first condition:

      • Each letter must appear at least twice within the string.
      • At least one whitespace must be present to enable replacing letters with whitespace. (For instance, in the string "AAB_CBC").
    2. Under the second condition:

      • Not any whitespace within the string.
      • Therefore, all letters must occur in pairs, such as "AA" within the string (for example, "AAABBBCCCC").
    def happyLadybugs(b):
        space = b.count('_')
        
        bug = {i:b.count(i) for i in set(b)}
        
        
        if all([bug[i] >= 2 for i in bug if i != "_"]) and space>0 or all([i*2 in b for i in bug if i != "_"]):
            
            return 'YES'
            
        else:
            return 'NO'