Top Description Constructors Methods
info.debatty.java.stringsimilarity

public Class LongestCommonSubsequence

extends Object
implements StringDistance
Class Inheritance
All Implemented Interfaces
info.debatty.java.stringsimilarity.interfaces.StringDistance, java.io.Serializable
Annotations
@Immutable
Imports
info.debatty.java.stringsimilarity.interfaces.StringDistance, net.jcip.annotations.Immutable

The longest common subsequence (LCS) problem consists in finding the longest subsequence common to two (or more) sequences. It differs from problems of finding common substrings: unlike substrings, subsequences are not required to occupy consecutive positions within the original sequences. It is used by the diff utility, by Git for reconciling multiple changes, etc. The LCS distance between Strings X (length n) and Y (length m) is n + m - 2 |LCS(X, Y)| min = 0 max = n + m LCS distance is equivalent to Levenshtein distance, when only insertion and deletion is allowed (no substitution), or when the cost of the substitution is the double of the cost of an insertion or deletion. ! This class currently implements the dynamic programming approach, which has a space requirement O(m * n)!
Author
Thibault Debatty

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final double

Returns:

the LCS distance between strings s1 and s2, computed as |s1| + |s2| - 2 * |LCS(s1, s2)|
distance
(final String
The first string to compare.
s1
,
final String
The second string to compare.
s2
)

Implements info.debatty.java.stringsimilarity.interfaces.StringDistance.distance.

Return the LCS distance between strings s1 and s2, computed as |s1| + |s2| - 2 * |LCS(s1, s2)|.

public final int

Returns:

the length of LCS(s1, s2)
length
(final String
The first string to compare.
s1
,
final String
The second string to compare.
s2
)

Return the length of Longest Common Subsequence (LCS) between strings s1 and s2.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

LongestCommonSubsequenceback to summary
public LongestCommonSubsequence()

Method Detail

distanceback to summary
public final double distance(final String s1, final String s2)

Implements info.debatty.java.stringsimilarity.interfaces.StringDistance.distance.

Return the LCS distance between strings s1 and s2, computed as |s1| + |s2| - 2 * |LCS(s1, s2)|.

Parameters
s1:String

The first string to compare.

s2:String

The second string to compare.

Returns:double

the LCS distance between strings s1 and s2, computed as |s1| + |s2| - 2 * |LCS(s1, s2)|

Exceptions
NullPointerException:
if s1 or s2 is null.
lengthback to summary
public final int length(final String s1, final String s2)

Return the length of Longest Common Subsequence (LCS) between strings s1 and s2.

Parameters
s1:String

The first string to compare.

s2:String

The second string to compare.

Returns:int

the length of LCS(s1, s2)

Exceptions
NullPointerException:
if s1 or s2 is null.