LeetCode 1945. Sum of Digits of String After Convert in F#

URL

https://leetcode.com/problems/sum-of-digits-of-string-after-convert/description/?envType=daily-question&envId=2024-09-03

Code

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

let getLucky (s: string) (k: int) : int =
    let rec getLucky' i cs acc =
        if i >= k then
            acc
        else
            match cs with
            | [] -> failwith "never reach here"
            | _ :: [] -> acc
            | _ ->
                let acc = cs |> List.map (fun c -> int c - int '0') |> List.sum
                getLucky' (i + 1) (acc |> string |> Seq.toList) acc

    let cs =
        s
        |> Seq.map (fun c -> int c - int 'a' + 1)
        |> System.String.Concat
        |> Seq.toList

    getLucky' 0 cs 0