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.
Using associateBy to create maps which reduce the count.
We're looking to elminate anything that has size > 2 or a minimum frequency > 1
funisValid(s:String):String{/* First, create a <char, frequency1> histogram */valn=mutableMapOf<Char,Int>()s.associateByTo(n,{it},{n.getOrDefault(it,0)+1})/* Second, create another one <frequency1, frequency2>*/valk=mutableMapOf<Int,Int>()n.values.associateByTo(k,{it},{k.getOrDefault(it,0)+1})/*What's left is a much more manageable (and sorted) map.*/returnwith(k){when{/* only one frequency */size==1->"YES"/* two frequencies? */size==2->{/* lowest frequency should not be > 1 */if(values.last()>1)"NO"/* if lowest is 1 it can be dropped */else"YES"}/* If there are > 2 frequencies dropping one won't help*/size>2->"NO"/* size 0 */else->"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 →
Kotlin - time: O(n) space: O(n+k)
Using
associateBy
to create maps which reduce the count. We're looking to elminate anything that hassize > 2
or a minimumfrequency > 1