Created
December 5, 2023 13:09
-
-
Save yodiz/3d76157ed5b2295b419ee5ba838da6ce to your computer and use it in GitHub Desktop.
longest common substring f#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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