my-solutions/codewars/ocaml/rank-up/consecutive-strings/main.ml

16 lines
619 B
OCaml
Raw Permalink Normal View History

let rec takeFirstN (xs : string list) (n : int) (acc : string) : string =
match n with
| 0 -> acc
| x -> takeFirstN (List.tl xs) (n-1) (acc ^ (List.nth xs 0));;
let rec joinK (xs : string list) (acc : string list) (k : int) : string list =
if List.length xs < k then acc
else joinK (List.tl xs) (acc @ [takeFirstN xs k ""]) k;;
let longestConsec (xs : string list) (k : int) : string =
if (List.length xs) == 0 || (List.length xs) < k || k <= 0
then ""
else
let pairs = joinK xs [] k in
List.fold_left (fun acc i -> if (String.length i) > (String.length acc) then i else acc) "" pairs;;