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.
Python solution with Collections.Counter. Time: O(N), Space: O(1)
"from collections import CounterdefisValid(s):counter_s=Counter(s)frequency=Counter(counter_s.values())iflen(frequency)==1:return""YES""iflen(frequency)==2:key1,key2=frequency.keys()val1,val2=frequency[key1],frequency[key2]# Case A: one char has freq=1 and it appears onceif(key1==1andval1==1)or(key2==1andval2==1):return""YES""# Case B: frequencies differ by 1 and higher one appears onceifabs(key1-key2)==1and(frequency[max(key1,key2)]==1):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 →
Python solution with Collections.Counter. Time: O(N), Space: O(1)