Skip to content

Instantly share code, notes, and snippets.

@uztbt
Created April 1, 2021 06:11
Show Gist options
  • Save uztbt/b85554b88480c4e9e48feaa57f5ffc7a to your computer and use it in GitHub Desktop.
Save uztbt/b85554b88480c4e9e48feaa57f5ffc7a to your computer and use it in GitHub Desktop.
def isValid(s: str) -> bool:
counter = Counter(s)
freqList = counter.most_common(None)
freqDict = defaultdict(list)
for freqTuple in freqList:
freqDict[freqTuple[1]].append(freqTuple[0])
fd = dict(freqDict)
if len(fd.keys()) == 1:
return "YES"
if len(fd.keys()) == 2:
[k1, k2] = sorted(fd.keys())
if k2 - k1 == 1 and len(fd[k2]) == 1:
return "YES"
if k1 == 1 and len(fd[k1])==1:
return "YES"
return "NO"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment