Sherlock and the Valid String

Sort by

recency

|

2064 Discussions

|

  • + 0 comments

    Insane python solution:

    def isValid(s):
        counts_dict = {}
        for c in s:
            counts_dict[c] = counts_dict.get(c,0) + 1
        
        counts = counts_dict.values()
        counts_len = len(counts)
        min_num = min(counts)
        max_num = max(counts)
        counts_sum = sum(counts)
        
        return "YES" if min_num*counts_len == counts_sum or min_num*counts_len+1 == counts_sum or max_num*(counts_len-1)+1==counts_sum else "NO"
    
  • + 0 comments

    A C++ solution with min/max:

    string isValid(string s) {
        std::map<char, int> uniq;
        for (const char c : s) {
            uniq[c]++;
        }
        auto max = std::max_element(uniq.begin(), uniq.end(), 
            [](std::pair<char, int> el1, std::pair<char, int> el2){
                 return el1.second < el2.second;
                 });
        auto min = std::min_element(uniq.begin(), uniq.end(),
            [](std::pair<char, int> el1, std::pair<char, int> el2){
                 return el1.second < el2.second;
                 });
    
        if (std::all_of(uniq.begin(), uniq.end(), [max](const auto el){ return el.second == max->second; } )) {
            return "YES";
        }
        max->second--;
        if (std::all_of(uniq.begin(), uniq.end(), [min](const auto el){ return el.second == min->second; } )) {
            return "YES";
        }
        max->second++;
        min->second--;
        if (std::all_of(uniq.begin(), uniq.end(), [max](const auto el){ return el.second == max->second || el.second == 0; } )) {
            return "YES";
        }
        return "NO";
    }
    
  • + 0 comments

    I wrote an exhaustive approach by finding all the invalid patterns, hahahaha

    https://drive.google.com/file/d/1nFrYgtfssjbG8yOfxTytgTyc6gkrzI8A/view?usp=sharing

  • + 0 comments

    Python 3 solution:

    def isValid(s):
        # Write your code here
        char_freq: dict[str, int] = {}
        freq_count: dict[int, int] = {}
        for char in s:
            char_freq[char] = char_freq.get(char, 0) + 1
        for freq in char_freq.values():
            freq_count[freq] = freq_count.get(freq, 0) + 1
        # reject if more than 2 different frequencies (-1 in any cannot become uniform)
        if len(freq_count.keys()) > 2:
            return "NO"
        # accept if all frequencies are the same / uniform
        if len(freq_count.keys()) == 1:
            return "YES"
        freqs: list[int] = list(sorted(freq_count.keys()))
        big_freq = freqs[1]
        small_freq = freqs[0]
        # edge case for smaller freq is single char
        if small_freq == 1 and freq_count[small_freq] == 1:
            return "YES"
        # when there is only 1 character type with freq = big_freq, 
        # and removing 1 of that char will make the freq_count uniform
        if freq_count[big_freq] == 1 and big_freq == small_freq + 1:
            return "YES"
        return "NO"
    

    Java 8 solution:

    public static String isValid(String s) {
    // Write your code here
        Map<Character, Long> charFreq = s.codePoints().mapToObj(x -> (char) x).collect(Collectors.groupingBy(x -> x, Collectors.counting()));
        Map<Long, Long> freqCount = charFreq.values().stream().collect(Collectors.groupingBy(x -> x, Collectors.counting()));
        int keySetSize = freqCount.size();
        // accept if all frequencies are the same / uniform
        if (keySetSize < 2) {
            return "YES";
        }
        // reject if more than 2 different frequencies (-1 in any cannot become uniform)
        if (keySetSize > 2) {
            return "NO";
        }
        Long[] freqs = freqCount.keySet().toArray(new Long[0]);
        int bigFreqIndex = freqs[1] > freqs[0] ? 1 : 0;
        long smallFreq = freqs[1 - bigFreqIndex];
        long bigFreq = freqs[bigFreqIndex];
        // edge case for smaller freq is single char
        if (smallFreq == 1 && freqCount.get(smallFreq) == 1) {
            return "YES";
        }
        // when there is only 1 character type with freq = big_freq, 
        // and removing 1 of that char will make the freq_count uniform
        if (bigFreq == smallFreq + 1 && freqCount.get(bigFreq) == 1l) {
            return "YES";
        }
        return "NO";
    }
    
  • + 0 comments

    my c# code like this:

       public static string isValid(string s)
        {
            var temp = s.GroupBy(x=> x).GroupBy(x=> x.Count());
            return temp.Where(x=> x.Count() == 1).Count() == 1 ? "YES" : "NO";
        }
    

    but it failed for test cases 3,5,14