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.
fromcollectionsimportCounterdefisValid(s):sv=sorted(Counter(s).values())#Countcharacterfrequenciesandsortthem# If all frequencies are the same, it's validiflen(set(sv))==1:return"YES"# Check cases where one frequency can be removed or adjusted to match the othersifsv[0]==1andsv[1]==sv[-1]:return"YES"# Check if we can make it valid by reducing the highest frequency by oneifsv[-1]-sv[-2]==1andsv[0]==sv[-2]:return"YES"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 →