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
|
2041 Discussions
|
Please Login in order to post a comment
here f1==1 is for edge case - where only single alphabet is in string while all other have same frequency
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;
class Result {
//To remove one element then count of the number has to be //consecutivem i.e,, key and the number of time it repeated has to be one //i.e., value. if ((key1 + 1 == key2 && value2 == 1) || (key2 + 1 == key1 && value1 == 1)) { return "YES"; } return "NO"; }
}
}
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']++; }
}