Skip to main content

LeetCode 3713. Longest Balanced Substring I in F#

Published 1 min readMarkdown

URL

https://leetcode.com/problems/longest-balanced-substring-i/description/?envType=daily-question&envId=2026-02-12

Code

https://github.com/syohex/dotnet-study/blob/main/fsharp/leetcode/challenge/202602/longest_balanced_substring1/main.fsx

let longestBalanced (s: string) : int =
    let isBalanced cs =
        cs
        |> Seq.fold
            (fun (acc, count) c ->
                let v = Map.tryFind c acc |> Option.defaultValue 0 |> ((+) 1)
                let acc = Map.add c v acc
                acc, max v count)
            (Map.empty, 0)
        |> fun (freq, count) -> freq |> Map.forall (fun _ v -> v = count)

    let rec f i acc =
        if i > s.Length then
            acc
        else
            let ok = s |> Seq.windowed i |> Seq.exists isBalanced
            if ok then f (i + 1) i else f (i + 1) acc

    f 1 0

No comments yet. Be the first to comment.