LeetCode 1814. Count Nice Pairs in an Array in F#
URL
Count Nice Pairs in an Array - LeetCode
Code
https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/problems/1814/main.fsx
let rev (n: int) : int =
let rec rev' n acc =
if n <= 0 then acc else rev' (n / 10) (acc * 10 + (n % 10))
rev' n 0
let countNicePairs (nums: int list) : int =
let modulo = 1_000_000_007
let rec countNicePairs' diffs dp acc =
match diffs with
| [] -> acc
| h :: t ->
match Map.tryFind h dp with
| None -> countNicePairs' t (Map.add h 1 dp) acc
| Some(v) -> countNicePairs' t (Map.add h (v + 1) dp) ((acc + v) % modulo)
let diffs = nums |> List.map (fun n -> n - rev n)
countNicePairs' diffs Map.empty 0