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):counter=Counter(s)# If all characters appear the same number of times, return "YES"iflen(set(counter.values()))==1:return"YES"counter_copy1=counter.copy()max_key=max(counter_copy1,key=lambdax:counter_copy1[x])counter_copy1[max_key]-=1# If all characters appear the same number of times after removing one character, return "YES"iflen(set(counter_copy1.values()))==1:return"YES"counter_copy2=counter.copy()min_key=min(counter_copy2,key=lambdax:counter_copy2[x])counter_copy2[min_key]-=1ifcounter_copy2[min_key]==0:delcounter_copy2[min_key]# If all characters appear the same number of times after removing one character, return "YES"iflen(set(counter_copy2.values()))==1:return"YES"# If none of the conditions are satisfied, return "NO"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 →
My Solution in python: