Sherlock and the Valid String

  • + 0 comments

    My Python solution:

    def isValid(w):
        freq_counter = Counter(w)
        freq_freq = Counter(freq_counter.values())
    
        if len(freq_freq) == 1:
            return "YES"
        elif len(freq_freq) == 2:
            (k1, v1), (k2, v2) = freq_freq.items()
            if v1 == 1 and (k1 - 1 == k2 or k1 == 1) or v2 == 1 and (k2 - 1 == k1 or k2 == 1):
                return "YES"
        return "NO"