Sherlock and the Valid String

Sort by

recency

|

105 Discussions

|

  • + 0 comments

    java

    public static String isValid(String s) {
                // Write your code here
                HashMap<Character , Integer> charFreq = new HashMap<>() ;
                for(char c:s.toCharArray())
                {
                    int count = charFreq.getOrDefault(c, 0) +1 ;
                    charFreq.put(c, count);
                }
                
                HashMap<Integer,Integer> freqCount = new HashMap<>();
                //freq => count 
                // 2   => 2   : aabbccc (a freq = 2 and b freq =2 so freq 2 => have count 2)  
                for(int freq : charFreq.values())
                {
                    int count = freqCount.getOrDefault(freq , 0)+1 ;
                    freqCount.put(freq , count);
                }
                
                if(freqCount.size() == 1)
                {
                    return "YES" ;
                }
                
                if(freqCount.size() == 2)
                {
                    int i =0 , f1 = 0 , f2 = 0 , c1 = 0 , c2 = 0 ;
                    for(int freq : freqCount.keySet())
                    {
                        if(i == 0)
                        {
                            f1 = freq ;
                            c1 = freqCount.get(freq);
                        }else{
                            f2 = freq ;
                            c2 = freqCount.get(freq);
                        }
                        i++ ;
                    }
                    
                    if((c1 == 1 && f1 == 1) || (c2 == 1 && f2 == 1))
                    { 
                        //int this condition size == 2 (only 2 frequecy)
                        // so if any freq == 1 and it's count == 1
                        // then if we remove that so it become valid 
                        // so it's a "YES" 
                        return "YES";
                    }
                    
                    if(Math.abs(f1 -f2) == 1)
                    {
                        //frequecy diffrence should be 1 
                        // example f1 = 2 and f2 = 3 => so if we remove 1 from f2 
                        // f2 = (3 -1) = 2 
                        // it can be  become valid string 
                        // can be not deffinatly 
                        /*
                            The two frequencies differ by exactly 1
                            The higher frequency (3) appears only once
                            We can remove 1 character from higher frequency
                             to make its frequency = 2
                             All characters would then have frequency 2
                        */
                                           
                        if((c1 == 1 && f1 > f2) || (c2 == 1 && f2 > f1))
                        {
                            /*
                            //eg => "aabbb"(true)
                                f1 = 2 , c1 = 1
                                f2 = 3 ,c2 = 1
                            
                             or "aabbbccc" (false)
                             f1 = 2 , c1 = 1 
                             f2 = 3 , c2 = 2
                             */
                             // if count = 1 
                             //then it freqncy shoubld be greter (+1)
                             //becouse remove there by one make the string valid 
                             //if count = 1 and freqncy is smaller ex => 2
                             // then even 2-1 = 1 
                             // one charecter still left 
                             //so not a valid string 
                            return "YES";
                        }
                    }
                    
                }
                
               
                return "NO"; // if freqCount greter than 2 then it's not valid 
                            // eg. => 2 , 1 , 3 => not valid
    
            }
    
  • + 0 comments

    Java 8 Solution

    Used HashMap to solve it.

    public static String isValid(String s) {
        // Write your code here
        int freq=0,count=0;
        Map<Character,Integer> frqMap = new HashMap<Character,Integer>();
        Map<Integer,Integer> higMap=new HashMap<>();
        for(char c: s.toCharArray()){
            frqMap.put(c, frqMap.getOrDefault(c, 0)+1);
        }
        
        for(int i:frqMap.values()){
            higMap.put(i, higMap.getOrDefault(i, 0)+1);
        }
        
        for(int i:higMap.keySet()){
            if(freq<higMap.get(i))
                freq=i;
        }
        
        
        for(int i: frqMap.values()){
            if(i!=freq){
                if(i==1)
                    count++;
                else if(i<freq)
                    count+=freq-i;
                else
                    count+=i-freq;
            }
        }    
        
        if(count>1)
            return "NO";
        else
            return "YES";
    
        }
    
  • + 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'