Sherlock and the Valid String

Sort by

recency

|

103 Discussions

|

  • + 0 comments

    C# - For some reason, case 14 isn't working for me, but it's too large to manually test,. and it isn't showing me my output.

            var buckets = new Dictionary<char, int>();
            var arr = s.ToCharArray();
            
            foreach(var c in arr)
            {
                if(!buckets.ContainsKey(c))
                {
                    buckets.Add(c,1);
                }
                else
                {
                    buckets[c] += 1;
                }    
            }
            
            foreach(var c in buckets.Keys)
            {
                Console.WriteLine($"{c} {buckets[c]}");
                // Clone buckets
                if(buckets[c] == 1)
                {
                    Console.WriteLine(buckets[c]);
                    var tempBuckets = buckets.Where(item => item.Key != c);
                    tempBuckets = tempBuckets.OrderBy(item => item.Value);
                    if(tempBuckets.Count() == 0 || tempBuckets.First().Value == tempBuckets.Last().Value)
                    {
                        return "YES";   
                    }    
                }
                else
                {
                    buckets[c] -= 1;
                    if(buckets.Values.Max() == buckets.Values.Min())
                    {
                        return "YES";   
                    }
                    buckets[c] += 1;                
                }    
            }
            
            return "NO";
    
  • + 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