Skip to content

Commit

Permalink
Merge f9233b6 into b8a44a7
Browse files Browse the repository at this point in the history
  • Loading branch information
vickumar1981 committed Jun 8, 2021
2 parents b8a44a7 + f9233b6 commit 4f79236
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.github.vickumar1981.stringdistance.impl

private[stringdistance] trait LongestCommonSeqImpl {
private def lcs[T](x: Array[T], y: Array[T], m: Int, n: Int): Int = {
if (m == 0 || n == 0) 0
else if (x(m - 1) == y(n - 1)) 1 + lcs(x, y, m - 1, n - 1)
else math.max(lcs(x, y, m, n - 1), lcs(x, y, m - 1, n))
protected def longestCommonSeq[T](s1: Array[T], s2: Array[T]): Int = {
val m = s1.length
val n = s2.length
val T = Array.ofDim[Int](m + 1, n + 1)
for (i <- 1 to m) {
for (j <- 1 to n) {
if (s1(i - 1) == s2(j - 1)) {
T(i)(j) = T(i - 1)(j - 1) + 1
} else {
T(i)(j) = math.max(T(i - 1)(j), T(i)(j - 1))
}
}
}
T(m)(n)
}

protected def longestCommonSeq[T](s1: Array[T], s2: Array[T]): Int =
lcs(s1, s2, s1.length, s2.length)
}

0 comments on commit 4f79236

Please sign in to comment.