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){if(s.length()==1)return"YES";// Count the letters int[]l=newint[26];for(inti=0;i<s.length();++i){++l[s.charAt(i)-97];}// Sort the Letter countArrays.sort(l);// Check for deviation.booleanhasAdjusted=false;intgoal=(l[l.length-1]!=l[l.length-2])?l[l.length-2]:l[l.length-1];for(inti=l.length-1;i>=0;--i){if(l[i]!=0){if(l[i]>goal){if(hasAdjusted||l[i]-goal>1)return"NO";hasAdjusted=true;}elseif(l[i]<goal){if(hasAdjusted||l[i]!=1)return"NO";hasAdjusted=true;}}}return"YES";}
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 8