Sherlock and the Valid String

Sort by

recency

|

2070 Discussions

|

  • + 0 comments

    solution in Ruby:

    def isValid(s)
       char_count = Hash.new(0)
       s.chars.each do |c|
          char_count[c]+=1
       end
       
       count_check = Hash.new(0)
       char_count.values.each do |v|
          count_check[v] +=1
       end
       
       return 'YES' if count_check.length == 1
       return 'NO' if count_check.length > 2
       
       return 'NO' if !count_check.values.include?(1)
       
       big_key = count_check.keys.max
       small_key = count_check.keys.min
       
       return 'YES' if small_key == 1 && count_check[1] == 1
       return 'YES' if count_check[big_key] == 1 && (big_key - small_key) == 1
       
        'NO' 
       
       
    end
    
  • + 0 comments

    Here is solution in python, java, c++, c and javascript programming - https://programmingoneonone.com/hackerrank-sherlock-and-the-valid-string-solution.html

  • + 0 comments

    Possible issue in test case validation

    For the input aaaabbbbccc, frequencies are a=4, b=4, c=3. Removing any single character results in frequencies (4,4,2), (3,4,3), or (4,3,3), none of which are equal.

    So by the problem statement (“remove exactly one character to equalize frequencies”), this string should be invalid. However, it is accepted because the checker only verifies “two frequencies with difference 1 and one occurring once”, which is not sufficient in this case.

    This looks like a test-case or validation logic issue rather than a solution bug.

  • + 0 comments
    public static String isValid(String s) {
    
        int []rows = new int[26];
        for(int i=0; i<s.length();i++) {
            rows[s.charAt(i) - 'a']++;
        }
    
        HashMap<Integer, Integer> bank = new HashMap<>();
        for(int i=0; i<rows.length;i++) {
            if(rows[i] > 0) {
                bank.put(rows[i], bank.getOrDefault(rows[i], 0)+1);
            }
        }
    
        System.out.print(bank);
    
        if(bank.size() == 1) return "YES";
        else if(bank.size() == 2) {
            List<Integer> store = new ArrayList(bank.keySet());
            Collections.sort(store);
            if(store.get(0) == 1 && bank.get(1) == 1) return "YES";
            if(store.get(0) == store.get(1)-1 && 
                (bank.get(store.get(0)) == 1 || bank.get(store.get(1)) == 1 )) return "YES";
        }
        return "NO";
    
    }
    
  • + 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""
    "