Sherlock and the Valid String

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