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.
Sherlock and the Valid String
Sherlock and the Valid String
Sort by
recency
|
2038 Discussions
|
Please Login in order to post a comment
def isValid(s): d = {i: s.count(i) for i in s} freqs = list(d.values()) freq_count = [] for f in freqs: if f not in freq_count: freq_count.append(f) if len(freq_count) == 1: return "YES"
elif len(freq_count) == 2: f1, f2 = freq_count if (freqs.count(f1)==1 and f1-f2== 1) or (freqs.count(f2)==1 and f2-f1==1): return "YES" elif (freqs.count(f1)==1 and f1==1) or (freqs.count(f2)==1 and f2==1): return "YES" else: return "NO" else: return "NO"
Through Brute Force:> cpp solution
string isValid(string s) { int freq[26]={0}; for (char c:s) { freq[c-'a']++; }
}
string isValid(string s) {
} } if(c>1 return "NO"; else return "YES";
JavaScript: