letscramblestr1str2=letletterLookup=str1|>Seq.countByid|>Map.ofSeqletrequiredCounts=str2|>Seq.countByidrequiredCounts|>Seq.forall(fun(letter,requiredCount)->matchletterLookup|>Map.tryFindletterwith|None->false// letter missing|Somecount->requiredCount<=count)
Edit: I had previously posted a version that had ever-so-slightly more performance but was more code. I'm also including that version below since it solves the problem differently.
letscramblestr1str2=letletterPool=str1|>Seq.sort|>List.ofSeqletrequiredLetters=str2|>Seq.sort|>List.ofSeqletreclooprequiredLettersletterPool=matchrequiredLetters,letterPoolwith|[],_->true// found all|_::_,[]->false// pool ran out|letter::_,next::poolwhenletter>next->looprequiredLetterspool// skip next in pool|letter::required,next::poolwhenletter=next->looprequiredpool|_,_->false// letter not available in the poollooprequiredLettersletterPool
Nice! yeah -- I only sometimes think about the efficiencies of these. They're contrived and we're doing them for fun. Part of me cares, part of me doesn't haha
I agree. I would have posted a much more expressive version, but you beat me to it! I did find an alternative way of solving the problem that was a little more expressive and nearly the same perf. I updated my post to include it.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
F#
Edit: I had previously posted a version that had ever-so-slightly more performance but was more code. I'm also including that version below since it solves the problem differently.
Here are the tests (console).
Nice! yeah -- I only sometimes think about the efficiencies of these. They're contrived and we're doing them for fun. Part of me cares, part of me doesn't haha
I agree. I would have posted a much more expressive version, but you beat me to it! I did find an alternative way of solving the problem that was a little more expressive and nearly the same perf. I updated my post to include it.