LeetCode 791. Custom Sort String in F#

URL

791. Custom Sort String

Code

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

open System

let customSortString (order: string) (s: string) : string =
    let table =
        order
        |> Seq.map (fun c -> int c - int 'a')
        |> Seq.indexed
        |> Seq.fold
            (fun (acc: int[]) (i, n) ->
                acc.[n] <- i
                acc)
            (Array.init 26 (fun _ -> 27))

    s |> Seq.sortBy (fun c -> table.[int c - int 'a']) |> String.Concat