We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
stringisValid(strings){intcnt[128]={};for(charc:s){cnt[c]++;}intmode=*std::max_element(begin(cnt),end(cnt));intmode_cnt=std::count(begin(cnt),end(cnt),mode);intmode_1_cnt=mode>1?std::count(begin(cnt),end(cnt),mode-1):0;// 1. only mode plus maybe one additional character (removing that character)// 2. one mode plus n * (mode-1) (removing 1 char from mode)if(mode*mode_cnt>=s.size()-1||mode+(mode-1)*mode_1_cnt==s.size()){return"YES";}else{return"NO";}}
Cookie support is required to access HackerRank
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 →
Simple C++ solution, need to calculate two modes.