LeetCode 17. Letter Combinations of a Phone Number in F#
URL
Letter Combinations of a Phone Number - LeetCode
Code
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) [] []