Sherlock and the Valid String

  • + 0 comments
    def isValid(s):
        cnt = Counter(Counter(s).values())
        val = list(cnt.keys())
        if len(cnt) == 1 or (len(cnt) == 2 and ((abs(val[0] - val[1]) == 1 and 1 in cnt.values()) or cnt[1]==1)):
            return 'YES'
        return 'NO'