Sherlock and the Valid String

Sort by

recency

|

2044 Discussions

|

  • + 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'
    }
    
  • + 0 comments

    PYTHON :

    def isValid(s):
        # Write your code here
        a = Counter(s)
        set1 = set(s)
        lst = sorted(list(a[p] for p in set1))
        lst1 = list(i-lst[0] for i in lst)
        
        if sum(lst1)==1 or sum(lst1)==0 or sum(lst1)==(len(lst1)-1)*lst1[-1]:
            return 'YES'
        else:
            return 'NO'
    
  • + 0 comments

    Less Spoiler: Possible scenarios: case1: aaabbbcccddd case2.abbbcccddd case3:aaaabbbcccdddd

    Spoiler: // case 1: uniqueFreqs == 1 means: All characters have the same frequency // example: "aabbccdd" all appear twice. The only distinct frequency is 2. // case 2: There are 2 distinct values and one of them is "1" and it occurs only once // example: aaaabbbbc the occurance of 4 (a and b) is 2 and the occurance of 1 is(c) is one. Remove the only letter which appears once // case 3: Frequencies differ by 1, and the higher frequency appears only once example: aaaabbbbccccc

    Java with array Hashmap is also an option I guess.

    public static String isValid(String s) {
        // Step 1: Count character frequencies
        int[] freq = new int[26];
        for (char c : s.toCharArray()) {
            freq[c - 'a']++;
        }
    
        // Step 2: Find the maximum frequency
        int maxFreq = 0;
        for (int f : freq) {
            if (f > maxFreq) {
                maxFreq = f;
            }
        }
    
        // Step 3: Count occurrences of each frequency
        int[] freqCounts = new int[maxFreq + 1];
        for (int f : freq) {
            if (f > 0) {
                freqCounts[f]++;
            }
        }
    
        // Step 4: Analyze frequency counts
        int uniqueFreqs = 0; // Count of unique(non zero) frequencies
        boolean canRemoveOne = false; // Whether we can remove one character to make it valid
    
        for (int i = 1; i < freqCounts.length; i++) {
            if (freqCounts[i] > 0) {
                uniqueFreqs++;
                // Check if we can remove one character to balance frequencies
                if (i < freqCounts.length - 1 && freqCounts[i + 1] == 1) {
                    canRemoveOne = true;
                }
            }
        }
    // case 1: uniqueFreqs == 1 means: All characters have the same frequency
        // example: "aabbccdd" all appear twice. The only distinct frequency is 2.
        // case 2: There are 2 distinct values and one of them is "1" and it occurs only once
        // example: aaaabbbbc  the occurance of 4 (a and b) is 2 and the occurance of 1 is(c) is one. Remove the only letter which appears once
        // case 3: Frequencies differ by 1, and the higher frequency appears only once example: aaaabbbbccccc
        
    		// Step 5: Return the result based on the cases
        if (uniqueFreqs == 1 || (uniqueFreqs == 2 && (freqCounts[1] == 1 || canRemoveOne))) {
            return "YES";
        }
    
        return "NO";
    }
    
  • + 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'