Created
February 3, 2022 11:48
-
-
Save ebresafegaga/5b4ef41752dc402cebe8e3a5a950968f to your computer and use it in GitHub Desktop.
Leetcode min deletions https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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