Sherlock and the Valid String

Sort by

recency

|

2041 Discussions

|

  • + 0 comments
    def isValid(s):
        cnt = Counter(Counter(s).values())
        val = list(cnt.keys())
        if len(cnt) == 1 or (len(cnt) == 2 and ((abs(val[0] - val[1]) == 1 and 1 in cnt.values()) or cnt[1]==1)):
            return 'YES'
        return 'NO'
    
  • + 0 comments

    here f1==1 is for edge case - where only single alphabet is in string while all other have same frequency

    def isValid(s):
        lst=list(s)
        set_s=set(s)
        dct={}
        for i in set_s:
            dct[i]=lst.count(i)
        freqs=list(dct.values())
        unique=set(freqs)
        if len(unique)==1:
            return 'YES'
        elif len(unique)==2:
            f1,f2=sorted(unique)
            count_f1=freqs.count(f1)
            count_f2=freqs.count(f2)
            if count_f1==1 and (abs(f2-f1)==1 or f1==1):
                return 'YES'
            elif count_f2==1 and abs(f2-f1)==1:
                return 'YES'     
        return 'NO'
    
  • + 0 comments

    import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;

    class Result {

    /*
     * Complete the 'isValid' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */
    
    public static String isValid(String s) {
        //Convert to array to get each character
      char[] charArr =  s.toCharArray();
    
            //create hashmap to store the each letters and theri count
    java.util.Map<Character,Integer> charMap = new java.util.HashMap<>();
    for(char c : charArr){
        charMap.put(c, charMap.getOrDefault(c,0)+1);
    }
    
    //Now we want with the same count how many letters are present.
        //So we will get the number of letters with the same count as key and //their count as value
    java.util.Map<Integer,Integer> countMap = new java.util.HashMap<>();
    
    for(int count : charMap.values() ){
        countMap.put(count,countMap.getOrDefault(count, 0)+1 );
    }
    
        //If size is one then all letters have same count
    if(countMap.size()==1){
        return "YES";
    }
    
        //If size is more than 2 then even after removal of one letter it is not //able to get the solution
    if(countMap.size()>2){
        return "NO";
    }
    
    int key1 = (int) countMap.keySet().toArray()[0]; 
        int key2 = (int) countMap.keySet().toArray()[1]; 
    
        int value1 = (int) countMap.values().toArray()[0];
        int value2 = (int) countMap.values().toArray()[1];
    
                //If there is one letter with the one count then we can remove it to //get solution
    
        if ((key1 == 1 && value1 == 1) || (key2 == 1 && value2 == 1)) {
        return "YES";
    }
    

    //To remove one element then count of the number has to be //consecutivem i.e,, key and the number of time it repeated has to be one //i.e., value. if ((key1 + 1 == key2 && value2 == 1) || (key2 + 1 == key1 && value1 == 1)) { return "YES"; } return "NO"; }

    }

        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments

    def isValid(s): d = {i: s.count(i) for i in s} freqs = list(d.values()) freq_count = [] for f in freqs: if f not in freq_count: freq_count.append(f) if len(freq_count) == 1: return "YES"
    elif len(freq_count) == 2: f1, f2 = freq_count if (freqs.count(f1)==1 and f1-f2== 1) or (freqs.count(f2)==1 and f2-f1==1): return "YES" elif (freqs.count(f1)==1 and f1==1) or (freqs.count(f2)==1 and f2==1): return "YES" else: return "NO" else: return "NO"

  • + 0 comments

    Through Brute Force:> cpp solution

    string isValid(string s) { int freq[26]={0}; for (char c:s) { freq[c-'a']++; }

    sort(freq,freq+26);
    
    if ((freq[24]==0) && (freq[25]!=0))
    {
        return "YES";
    }
    int j=0;
    while(freq[j]==0)
    {
        j++;
    }
    if (freq[j]==freq[25])
    {
        return "YES";
    }
    
    else if ((freq[j]==1) &&  freq[j+1]==freq[25])
    {
        return "YES";
    }
    
    else if((freq[25]-freq[j]==1) && freq[j]==freq[24])
    {
        return "YES";
    }
    
    else
    {
        return "NO";
    }
    

    }