Sherlock and the Valid String

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