LeetCode 2785. Sort Vowels in a String in F#

URL

Sort Vowels in a String - LeetCode

Code

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

open System

let sortVowels (s: string) : string =
    let isVowel (c: char) : bool =
        match c with
        | 'a'
        | 'A'
        | 'e'
        | 'E'
        | 'i'
        | 'I'
        | 'o'
        | 'O'
        | 'u'
        | 'U' -> true
        | _ -> false

    let rec sortVowels' (cs: char list) (vs: char list) (acc: char list) =
        match cs with
        | [] -> acc |> List.rev |> String.Concat
        | h :: t ->
            if isVowel h then
                sortVowels' t (List.tail vs) ((List.head vs) :: acc)
            else
                sortVowels' t vs (h :: acc)

    let vowels = s |> Seq.filter isVowel |> Seq.sort |> Seq.toList
    sortVowels' (Seq.toList s) vowels []