LeetCode 2981. Find Longest Special Substring That Occurs Thrice I in F#

URL

https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i/description/?envType=daily-question&envId=2024-12-10

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202412/find_longest_special_substring_that_occurs_thrice1/main.fsx

let maximumLength (s: string) : int =
    let isSingleChar cs =
        match cs with
        | [] -> failwith "never reach here"
        | _ :: [] -> true
        | h :: t -> List.forall ((=) h) t

    let rec maximumLength' len cs acc =
        if len >= s.Length then
            acc
        else
            let ok =
                List.windowed len cs
                |> List.filter isSingleChar
                |> List.countBy id
                |> List.exists (fun (_, count) -> count >= 3)

            maximumLength' (len + 1) cs (if ok then len else acc)

    maximumLength' 1 (Seq.toList s) -1