LeetCode 1624. Largest Substring Between Two Equal Characters in F#

URL

https://leetcode.com/problems/largest-substring-between-two-equal-characters/description/?envType=daily-question&envId=2023-12-31

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/problems/1624/main.fsx

open System

let maxLengthBetweenCharacters (s: string) : int =
    let rec maxLengthBetweenCharacters' cs m acc =
        match cs with
        | [] -> acc
        | (i, h) :: t ->
            match Map.tryFind h m with
            | None -> maxLengthBetweenCharacters' t (Map.add h i m) acc
            | Some(v) -> maxLengthBetweenCharacters' t m (Math.Max(acc, i - v - 1))

    let cs = s |> Seq.indexed |> Seq.toList
    maxLengthBetweenCharacters' cs Map.empty -1