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){// Write your code hereHashSet<Character>stringSet=newHashSet<>();List<Character>stringList=newArrayList<>();HashMap<Character,Integer>frequency=newHashMap<>();HashMap<Integer,Integer>freqCount=newHashMap<>();for(charc:s.toCharArray()){stringSet.add(c);stringList.add(c);}for(Characterch:stringSet){frequency.put(ch,Collections.frequency(stringList,ch));}intsize=newHashSet<>(frequency.values()).size();if(size==1){return"YES";}for(Integervalue:frequency.values()){freqCount.put(value,freqCount.getOrDefault(value,0)+1);}if(freqCount.size()==2){if(Collections.min(freqCount.keySet())==1&&freqCount.get(Collections.min(freqCount.keySet()))==1){return"YES";}elseif(Collections.max(freqCount.keySet())-Collections.min(freqCount.keySet())==1&&freqCount.get(Collections.max(freqCount.keySet()))==1){return"YES";}else{return"NO";}}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 →