Sherlock and the Valid String

  • + 0 comments

    Counter is nice.

    def isValid(s):
        counts = dict(Counter(s))
        count_freqs = dict(Counter(counts.values()))
        if len(count_freqs) == 1:
            return 'YES'
        elif len(count_freqs) > 2:
            return 'NO'
        elif count_freqs.get(1, 0) == 1:
            return 'YES'
        elif abs(list(count_freqs.keys())[-1] - list(count_freqs.keys())[0]) == 1 and count_freqs[max(list(count_freqs.keys()))] == 1:
            return 'YES'
        
        return 'NO'