Skip to content

Instantly share code, notes, and snippets.

View noahlz's full-sized avatar
🕵️‍♂️
Secret Stuff for SEI Novus. I might take 1 - 3 days to respond.

Noah Zucker noahlz

🕵️‍♂️
Secret Stuff for SEI Novus. I might take 1 - 3 days to respond.
View GitHub Profile
/**
* non-performant solution to https://twitter.com/Al_Grigor/status/1357028887209902088
* scala> partitionCount("aaaabbbcca")
* res0: List[(Char, Int)] = List((a,4), (b,3), (c,2), (a,1))
* PS see "stringly" for a better solution in clojure.
* Is there a scala equivalent to this? Feel free to comment.
* (->> (partition-by identity s) (map frequencies) seq))
*/
def partitionCount(s: String): List[(Char, Int)] = {
s.toList.foldLeft(List.empty[(Char, Int)]) { (agg, c) =>