Problem :

https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem


My Solution :

#!/usr/bin/env python3

from collections import defaultdict


def isValid(s):
    char_dic, count_dic = defaultdict(int), defaultdict(int)
    for char in s:
        char_dic[char] += 1
    for count in char_dic.values():
        count_dic[count] += 1
    if len(count_dic) == 1:
        return 'YES'
    if len(count_dic) > 2:
        return 'NO'
    m, M = sorted(count_dic)
    if count_dic[1] == 1:
        return 'YES'
    if M - m == 1 and count_dic[M] == 1:
        return 'YES'
    return 'NO'


s = input().strip()
result = isValid(s)
print(result)