Sherlock and the Valid String

  • + 0 comments

    Python solution using dictionary and set

    def isValid(s):
        # Write your code here
        holder = {}
        for char in s:
            if char in holder:
                holder[char] += 1
            else:
                holder[char] = 1
        val = list(holder.values())
        val.sort()
        test1 = set(val[1:])
        test2 = set(val[:-1])
        test2.add(val[-1]-1)
        result = "YES" if len(test1) == 1 or len(test2) == 1 else "NO"
        return result