Skip to main content

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

Published 1 min readMarkdown

URL

Unique Length-3 Palindromic Subsequences - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202511/unique_length3_palindromic_subsequences/main.fsx

let findStartAndLastPosition (c: char) (s: string) : int * int =
    s
    |> Seq.indexed
    |> Seq.fold
        (fun (first, last) (i, d) ->
            if c = d then
                if first = -1 then i, last else first, i
            else
                first, last)
        (-1, -1)

let countPalindromicSubsequence (s: string) : int =
    s
    |> Set.ofSeq
    |> Set.map (fun c -> findStartAndLastPosition c s)
    |> Set.filter (fun (_, last) -> last <> -1)
    |> Set.fold
        (fun acc (first, last) ->
            let uniqueChars = s.Substring(first + 1, last - first - 1) |> Set.ofSeq |> Set.count
            acc + uniqueChars)
        0

No comments yet. Be the first to comment.

More from this blog