Sherlock and the Valid String

Sort by

recency

|

2057 Discussions

|

  • + 0 comments

    // my code is not passing only one test case can anyone help why ?

    string isValid(string s) {

    // cnt[i] stores frequency of character i+'a' int cnt[26] = {0}, n = s.length();

    for (int i = 0; i < n; i++) { // increasing frequency cnt[s[i] - 'a']++; }

    for (int i = 0; i < 26; i++) { // if character i+'a' is not present in string continue if (cnt[i] == 0) continue;

      cnt[i]--;
    
    // if we insert all positive frequencies into a set, it should contain
    // only 1 element if string is now valid
    set<int> myset;
    
    // insert remaining positive frequencies into set
    for (int j = 0; j < 26; j++) {
      if (cnt[j]) myset.insert(cnt[j]);
    }
    
    // if set size is 1, string is now valid
    if (myset.size() <= 1) return "YES";
    
    // increase the frequency back again
      cnt[i]++;
    

    }

    return "NO";

    }

  • + 0 comments

    My C++ solution:

    string isValid(string s) {
        map<char,int> charmap;
        for (char c : s) {
            charmap[c]++;
        }
        
        map<int, int> freqCounts;
        for (auto const& pair : charmap) {
            freqCounts[pair.second]++;
        }
    
        if (freqCounts.size() == 1) {
            return "YES";
        }
    
        if (freqCounts.size() == 2) {
            vector<pair<int, int>> freqs(freqCounts.begin(), freqCounts.end());
            int freq1 = freqs[0].first;
            int count1 = freqs[0].second;
            int freq2 = freqs[1].first;
            int count2 = freqs[1].second;
    
            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

    include

    include

    include

    include

    include

    include

    using namespace std;

    int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string str;cin>>str; vectorcnt(26,0);unordered_mapmp; for(char &c : str) cnt[c - 'a']++; for(int i=0;i<26;++i){ if(cnt[i]>0){ mp[cnt[i]]++; } } if(mp.size()==1) { cout << "YES\n"; } else if(mp.size()==2){ int fre1,fre2,b1,b2; auto it = mp.begin(); fre1 = it->first;b1 = it->second; it++; fre2 = it->first;b2 = it->second; if((fre1==1&&b1==1)||(fre2==1&&b2==1)|| (abs(fre1-fre2)==1&&(b1==1||b2==1))){ cout << "YES\n"; } else cout << "NO\n"; } else cout << "NO\n"; return 0; }


  • + 0 comments

    Test case 14 consists of the letters 'a' through 'y', each appearing 4000 times, in random order. It is expecting a "YES" return, even though by the definitions set forth this is an invalid string.

    Either something went wrong in the storage backend for the test cases and a letter got chopped off somehow, or this problem is expecting an incorrect answer.

    A correct answer can simply use a character frequency count and some conditionals.

  • + 0 comments

    I thought this one would be super easy but got humbled after I couldn't solve it in 15 minutes. Here is my Python solution!

    def isValid(s):
        s = Counter(s)
        frequencies = s.values()
        common = Counter(frequencies).most_common(1)[0][0]
        removed = False
        for frequency in frequencies:
            if abs(frequency - common) == 1 or (frequency != common and frequency == 1):
                if removed:
                    return "NO"
                else:
                    removed = True
            elif abs(frequency - common) > 1:
                return "NO"
        return "YES"