You are viewing a single comment's thread. Return to all comments →
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; }
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Sherlock and the Valid String
You are viewing a single comment's thread. Return to all 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; }