Sherlock and the Valid String

  • + 0 comments
    def isValid(s):
        # Write your code here
        count = Counter(s).values()
        countvalues = Counter(count)
        
        if len(countvalues) > 2: 
            return 'NO'
        elif len(countvalues) == 1:
            return 'YES'
        val = min(countvalues.keys())
        val2 = max(countvalues.keys())
        if abs(val2 - val) > 1:
            if val != 1 or countvalues[1] > 1:
                return 'NO'
                
        if min(countvalues.values()) > 1:
            return 'NO'
            
        return 'YES'