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.
publicstaticStringisValid(Strings){Map<String,Integer>cntMap=newHashMap<>();intmaxCnt=0;intmaxVal=0;intminVal=Integer.MAX_VALUE;// Set maxVal and cntMapfor(inti=0;i<s.length();i++){Stringkey=s.substring(i,i+1);intval=cntMap.compute(key,(k,v)->v==null?1:v+1);if(val>maxVal)maxVal=val;}// Set minVal and maxCntfor(intvalue:cntMap.values()){if(value==maxVal)maxCnt++;if(value<minVal)minVal=value;}booleanvalid=maxVal==minVal||minVal==1&&maxCnt==cntMap.size()-1||maxVal-minVal==1&&maxCnt==1;returnvalid?"YES":"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 →
Java Solution