Skip to content

Instantly share code, notes, and snippets.

@yodiz
Created December 5, 2023 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yodiz/3d76157ed5b2295b419ee5ba838da6ce to your computer and use it in GitHub Desktop.
Save yodiz/3d76157ed5b2295b419ee5ba838da6ce to your computer and use it in GitHub Desktop.
longest common substring f#
//Skapa griden, det är en tabell med sträng 1 på x och sträng 2 på y
let longest_common_substring (word_a:string) (word_b:string) =
let cell = Array.init word_a.Length (fun _ -> Array.init word_b.Length (fun _ -> 0))
for i = 0 to word_a.Length - 1 do
for j = 0 to word_b.Length - 1 do
if word_a[i] = word_b[j] then
if i > 0 && j > 0 then
cell[i][j] <- cell[i-1][j-1] + 1
else
cell[i][j] <- 1
else
cell[i][j] <- 0
cell
longest_common_substring "fish" "hish"
|> Array.iter (fun x -> x |> Array.iter (fun x -> printf "%i " x); printfn "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment