Sherlock and the Valid String

Sort by

recency

|

2038 Discussions

|

  • + 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";
    }
    

    }

  • + 0 comments

    string isValid(string s) {

    int c=0;
    
    vector<int>vec;
    
    unordered_map<char , int>mp;
    
    for(int i=0;s[i];i++){
    
        if(mp.find(s[i]) == mp.end())
    
            mp.insert({s[i],1});
    
        else
    
            mp[s[i]]++;
    
    }
    
    for(auto it:mp){
    
        vec.push_back(it.second);
    
    }
    
    int l=vec[1];
    
    for(int x:vec){
    
        if(x==l)
    
            continue;
    
        else if(x>l||x<l){
    
            c++;
    
            continue;
    
        }
    
        else
    
            return "NO";
    

    } } if(c>1 return "NO"; else return "YES";

  • + 0 comments

    JavaScript:

    function isValid(s: string): string {
      const charFreq = new Map();
      
      for (let char of s) {
        charFreq.set(char, (charFreq.get(char) || 0) + 1);
      }
      
      const freqCount = new Map();
      for (let freq of charFreq.values()) {
        freqCount.set(freq, (freqCount.get(freq) || 0) + 1);
      }
      
      if (freqCount.size === 1) return "YES";
      if (freqCount.size > 2) return "NO";
      
      const [[freq1, count1], [freq2, count2]] = Array.from(freqCount.entries());
      
      // Check valid conditions
      return (
        (freq1 === 1 && count1 === 1) ||
        (freq2 === 1 && count2 === 1) ||
        (Math.abs(freq1 - freq2) === 1 && (count1 === 1 || count2 === 1))
      ) ? "YES" : "NO";
    }
    
  • + 0 comments
    #include <bits/stdc++.h>
    
    using namespace std;
    
    char s[1234567];
    
    int main() {
      scanf("%s", s);
      int n = strlen(s);
      vector <int> cnt(26, 0);
      for (int i = 0; i < n; i++) {
        cnt[s[i] - 'a']++;
      }
      for (int i = 0; i <= 26; i++) {
        if (cnt[i] == 0) {
          continue;
        }
        if (i < 26) {
          cnt[i]--;
        }
        vector <int> a = cnt;
        sort(a.begin(), a.end());
        int res = a[25];
        bool ok = true;
        for (int j = 0; j < 26; j++) {
          if (a[j] != 0 && a[j] != res) {
            ok = false;
            break;
          }
        }
        if (ok) {
          puts("YES");
          return 0;
        }
        if (i < 26) {
          cnt[i]++;
        }
    		///
      }
      puts("NO");
      return 0;
    }