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 hereintfreq=0,count=0;Map<Character,Integer>frqMap=newHashMap<Character,Integer>();Map<Integer,Integer>higMap=newHashMap<>();for(charc:s.toCharArray()){frqMap.put(c,frqMap.getOrDefault(c,0)+1);}for(inti:frqMap.values()){higMap.put(i,higMap.getOrDefault(i,0)+1);}for(inti:higMap.keySet()){if(freq<higMap.get(i))freq=i;}for(inti:frqMap.values()){if(i!=freq){if(i==1)count++;elseif(i<freq)count+=freq-i;elsecount+=i-freq;}}if(count>1)return"NO";elsereturn"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 Solution
Used HashMap to solve it.