LeetCode 17. Letter Combinations of a Phone Number in F#

URL

Letter Combinations of a Phone Number - LeetCode

Code

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

open System

let letterCombinations (digits: string) : string list =
    let table = [| "abc"; "def"; "ghi"; "jkl"; "mno"; "pqrs"; "tuv"; "wxyz" |]

    let rec letterCombinations' digits (acc: char list) ret =
        match digits with
        | [] ->
            let s = acc |> List.rev |> String.Concat
            s :: ret
        | h :: t ->
            let index = int h - int '2'

            table.[index]
            |> Seq.fold (fun ret c -> letterCombinations' t (c :: acc) ret) ret

    if digits.Length = 0 then
        []
    else
        letterCombinations' (Seq.toList digits) [] []