Skip to main content

LeetCode 2785. Sort Vowels in a String in F#

Published 1 min readMarkdown

URL

Sort Vowels in a String - LeetCode

Code

dotnet-study/fsharp/leetcode/challenge/202509/sort_vowels_in_a_string/main.fsx at master · syohex/dotnet-study

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

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

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

No comments yet. Be the first to comment.

More from this blog