Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
Created February 3, 2022 11:48
Show Gist options
  • Save ebresafegaga/5b4ef41752dc402cebe8e3a5a950968f to your computer and use it in GitHub Desktop.
Save ebresafegaga/5b4ef41752dc402cebe8e3a5a950968f to your computer and use it in GitHub Desktop.
let rec min count counts =
if count = 0 then 0
else if not (List.contains count counts) then
0
else
1 + min (count - 1) counts
let minDeletions (str : string) =
let step m elem =
if Map.containsKey elem m then
m |> Map.map (fun key value -> if key = elem then value + 1 else value)
else m |> Map.add elem 1
let counts = [ for s in str do s ] |> List.fold step Map.empty |> Map.toList |> List.map snd
let rec countDeletions l =
match l with
| [] -> (0, [])
| x :: xs ->
let count, xs = countDeletions xs
let m = min x xs
(m+count,x-m :: xs)
in
counts |> countDeletions |> fst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment