We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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"
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and the Valid String
You are viewing a single comment's thread. Return to all 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"