LeetCode 1930. Unique Length-3 Palindromic Subsequences in F#

URL

https://leetcode.com/problems/unique-length-3-palindromic-subsequences/description/?envType=daily-question&envId=2023-11-14

Code

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

let countPalindromicSubsequence (s: string) : int =
    let rec countPalindromicSubsequence' cs (s: char[]) acc =
        match cs with
        | [] -> acc
        | h :: t ->
            let first, last = Array.findIndex ((=) h) s, Array.findIndexBack ((=) h) s

            if first = last then
                countPalindromicSubsequence' t s acc
            else
                let uniqs = s.[(first + 1) .. (last - 1)] |> Set.ofArray |> Seq.length
                countPalindromicSubsequence' t s (acc + uniqs)

    let uniqChars = Set.ofSeq s |> Set.toList
    countPalindromicSubsequence' uniqChars (Seq.toArray s) 0