Sherlock and the Valid String

Sort by

recency

|

102 Discussions

|

  • + 0 comments
    from collections import Counter
    
    def isValid(s):
        sv = sorted(Counter(s).values())  # Count character frequencies and sort them
        
        # If all frequencies are the same, it's valid
        if len(set(sv)) == 1:
            return "YES"
        
        # Check cases where one frequency can be removed or adjusted to match the others
        if sv[0] == 1 and sv[1] == sv[-1]:
            return "YES"
        
        # Check if we can make it valid by reducing the highest frequency by one
        if sv[-1] - sv[-2] == 1 and sv[0] == sv[-2]:
            return "YES"
        
        return "NO"
    
  • + 0 comments
    from collections import Counter
    
    def isValid(s):
        c = dict(sorted(Counter(Counter(s).values()).items()))
        return 'YES' if (len(c) == 1 or (len(c) == 2 and ((list(c.keys())[1] - list(c.keys())[0] == 1 and list(c.values())[1] == 1) or c.get(1) == 1))) else 'NO'
    
  • + 0 comments

    Java 8

    public static String isValid(String s) {
            if (s.length()==1)
                return "YES" ;
            // Count the letters 
            int[] l = new int[26] ;
            for (int i=0; i<s.length(); ++i) {
                ++l[s.charAt(i)-97];
            }
            
            // Sort the Letter count
            Arrays.sort(l);
            
            // Check for deviation.
            boolean hasAdjusted = false ;
            int goal = (l[l.length-1]!=l[l.length-2])?l[l.length-2]:l[l.length-1];
            for (int i=l.length-1; i>=0; --i) {
                if (l[i] != 0 ) {
                    if (l[i] > goal) {
                        if (hasAdjusted || l[i] - goal > 1)
                            return "NO";
                        hasAdjusted = true;
                    } else if (l[i] < goal) {
                        if ( hasAdjusted || l[i] != 1)
                            return "NO";
                        hasAdjusted=true;
                    }
                }
            }
            
            return "YES" ;
        }
    
  • + 0 comments

    Here is HackerRank sherlock and the Valid String problem solution in Python, Java, C++, C and javascript

  • + 0 comments
    public static String isValid(String s) {
            // Write your code here
            HashSet <Character> stringSet = new HashSet<>();
            List <Character> stringList = new ArrayList<>();
            HashMap <Character, Integer> frequency = new HashMap<>();
            HashMap <Integer, Integer> freqCount = new HashMap<>();
            for (char c : s.toCharArray()) {
                stringSet.add(c);
                stringList.add(c);
            }
            for (Character ch : stringSet) {
                frequency.put(ch, Collections.frequency(stringList, ch));
            }
            int size = new HashSet<>(frequency.values()).size();
            if (size == 1) {
                return "YES";
            } 
            for (Integer value : frequency.values()) {
                freqCount.put(value, freqCount.getOrDefault(value, 0) + 1);
            }
            if (freqCount.size() == 2) {
                if (Collections.min(freqCount.keySet()) == 1 && freqCount.get(Collections.min(freqCount.keySet())) == 1) {
                    return "YES";
                } else if (Collections.max(freqCount.keySet())-Collections.min(freqCount.keySet())==1 && freqCount.get(Collections.max(freqCount.keySet())) == 1) {
                    return "YES";
                } else {
                    return "NO";
                }
            }
            return "NO";
        }