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.
defisValid(s):cts=Counter(s)#Countoccurencesofstringct2=Counter(cts.values())#Countuniqueoccurencesofstringoccurences#if only one item in ct2 then all occurences are equal, #if two items then check if diff is 1 and second most has a count of 1#Special case: if the second occurences of occurences equals 1, #it means the second 1 occurrence can be removed to make a valid string.kc=ct2.most_common()return'YES'iflen(kc)==1or(len(kc)==2and \
(((kc[1][0]-kc[0][0])==1orkc[1][0]==1)andkc[1][1]==1))else'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 →
Best one: