Sherlock and the Valid String

Sort by

recency

|

2066 Discussions

|

  • + 0 comments

    Python solution with Collections.Counter. Time: O(N), Space: O(1)

    "from collections import Counter
    
    def isValid(s):
        counter_s = Counter(s)
        frequency = Counter(counter_s.values())
    
        if len(frequency) == 1:
            return ""YES""
    
        if len(frequency) == 2:
            key1, key2 = frequency.keys()
            val1, val2 = frequency[key1], frequency[key2]
    
            # Case A: one char has freq=1 and it appears once
            if (key1 == 1 and val1 == 1) or (key2 == 1 and val2 == 1):
                return ""YES""
    
            # Case B: frequencies differ by 1 and higher one appears once
            if abs(key1 - key2) == 1 and (frequency[max(key1, key2)] == 1):
                return ""YES""
    
        return ""NO""
    "
    
  • + 0 comments

    I am using this java solution but still getting 2 test case failed anyone hlep me , thanks in advance

    Map<Character,Integer> ma=new HashMap<>();
    
            for(int i=0;i<s.length();i++)
            {
                if(!ma.containsKey(s.charAt(i)))
                {
                    ma.put(s.charAt(i),1);
                }
                else {
                    ma.computeIfPresent(s.charAt(i),(k,v)->++v);
                }
            }
    
             Integer[] op= ma.values().toArray(Integer[]::new);
    
            Arrays.sort(op);
    
            if(op[0]==op[op.length-1])
            {
                return "YES";
            }
      if(op[0]==1 && op[1]==op[op.length-1])
            {
               return "YES";
            }
           
                if(op[0]==op[1] && op[1]==op[op.length-2] && (op[1]==op[op.length-1]-1))
                {
                    return "YES";
                }
            
        
    
    return "NO";
        }
    
  • + 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