Sherlock and the Valid String

  • + 0 comments
    from collections import Counter
    
    def isValid(s):
        c = dict(sorted(Counter(Counter(s).values()).items()))
        return 'YES' if (len(c) == 1 or (len(c) == 2 and ((list(c.keys())[1] - list(c.keys())[0] == 1 and list(c.values())[1] == 1) or c.get(1) == 1))) else 'NO'