diff --git a/codewars/ocaml/rank-up/consecutive-strings/main.ml b/codewars/ocaml/rank-up/consecutive-strings/main.ml new file mode 100644 index 0000000..f59bede --- /dev/null +++ b/codewars/ocaml/rank-up/consecutive-strings/main.ml @@ -0,0 +1,15 @@ +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;;