Sherlock and the Valid String

  • + 0 comments

    def isValid(s): d = {i: s.count(i) for i in s} freqs = list(d.values()) freq_count = [] for f in freqs: if f not in freq_count: freq_count.append(f) if len(freq_count) == 1: return "YES"
    elif len(freq_count) == 2: f1, f2 = freq_count if (freqs.count(f1)==1 and f1-f2== 1) or (freqs.count(f2)==1 and f2-f1==1): return "YES" elif (freqs.count(f1)==1 and f1==1) or (freqs.count(f2)==1 and f2==1): return "YES" else: return "NO" else: return "NO"