Sherlock and the Valid String

Sort by

recency

|

2048 Discussions

|

  • + 0 comments

    python -

    def isValid(s):

    # Write your code here
    s_dict = {}
    count=0
    for i in s:
        if i in s_dict:
            s_dict[i]+=1
        else:
            s_dict[i]=1
    dict_val_list = list(s_dict.values())
    
    for i in range(1,len(s_dict)):
        if dict_val_list[0] == dict_val_list[i]:
            continue          
        else:
            if count==0:
                count+=1
            else:
                return 'NO'
    return 'YES'
    
  • + 0 comments

    The input "aabbbccc" is not a valid one cz at least 2 characters has to be removed to make this valid. But the editorial solutions consider this input as "valid". How's that possible? Am I missing something obvious here?

  • + 0 comments
     public static String isValid(String s) {
            Map<Character,Integer> map = new HashMap<>();
            int n = s.length();
            for(int i=0 ; i<n ; i++){
                map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0)+1);
            } 
            Map<Integer,Integer> freqMap = new HashMap<>();
            for(int fq : map.values())
            {
                freqMap.put(fq, freqMap.getOrDefault(fq, 0)+1);
            }
              if (freqMap.size() == 1) {
                return "YES"; 
            }
              if (freqMap.size() == 2) {
                for(int i : freqMap.values()){
                    if(i==1) return "YES";
                }
            }
    
                can any one explain why it is paasing only 18/20 test cases? also please give the failed test case.
    
                `        
        return "NO";
    
    }
    
  • + 0 comments

    Perl:

    sub isValid {
        my @s = split("", shift);
        
        my %h;
        my %c;
    
        foreach (@s) {
            $h{$_} +=1;
        }
        foreach (values(%h)) {
            $c{$_} +=1;
        }
    
        return "YES" if (keys(%c) == 1);    
        
        if (keys %c == 2) {
            my @keys = keys(%c);
            my ($freq1, $freq2) = @keys;
            my ($count1, $count2) = @c{@keys};
    
            if (($freq1 == 1 && $count1 == 1) || ($freq2 == 1 && $count2 == 1)) {
                return "YES";
            }
    
            if ((abs($freq1 - $freq2) == 1) && ($count1 == 1 || $count2 == 1)) {
                return "YES";
            }
        }
    
        return "NO";
    }
    
  • + 0 comments

    My answer with Typescript, can be increase [point] to increase number of character can be remove

    function isValid(s: string): string {
        // 0. count every char in [s]
        let hash = new Map<string, number>()
        for (let c of s) hash.set(c, (hash.get(c) || 0) + 1)
    
        // 1. define a [value_average] and [point] that can be decreate
        let values = Array.from(hash.values())
        let value_average = values[0]
        let point = 1
    
        // 2. loop from 1 to end, check value gap and decreate point
        //  -> if [point] is out, return 'NO'
        //  -> orelse return 'YES'
        for (let i = 1; i < values.length; i++) {
            let value = values[i]
    
            if (value != value_average) {
                if (point == 0) return 'NO'
                point--
            }
        }
    
        return 'YES'
    }