You are viewing a single comment's thread. Return to all 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"; }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and the Valid String
You are viewing a single comment's thread. Return to all comments →
Through Brute Force:> cpp solution
string isValid(string s) { int freq[26]={0}; for (char c:s) { freq[c-'a']++; }
}