The Longest common subsequence algorithm returns the length of the longest subsequence that two strings have in common. Two strings that are entirely different, return a value of 0, and two strings that return a value of the commonly shared length implies that the strings are completely the same in value and position
Note
Generally this algorithm is fairly inefficient, as for length m, n of the input
CharSequence's left and right respectively, the runtime of the
algorithm is O(m*n).
As of version 1.10, a more space-efficient of the algorithm is implemented. The new algorithm has linear space complexity instead of quadratic. However, time complexity is still quadratic in the size of input strings.
The implementation is based on Hirschberg's Longest Commons Substring algorithm (cited below).
For further reading see:
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final LongestCommonSubsequence | INSTANCE
The singleton instance. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private static int[] | Returns: last row of the dynamic-programming (DP) table for calculating the LCS ofleft and right.first input sequence. left, final CharSequence second input sequence. right)An implementation of "ALG B" from Hirschberg's CACM '71 paper. |
| private static String | Returns: the LCS ofleft and right.first input sequence. left, final CharSequence second input sequence. right)An implementation of "ALG C" from Hirschberg's CACM '71 paper. |
| public Integer | Returns: length of the longest common subsequence ofleft and right.first character sequence. left, final CharSequence second character sequence. right)Implements org. Computes the longest common subsequence similarity score of two |
| public CharSequence | Returns: the longest common subsequence found.first character sequence. left, final CharSequence second character sequence. right)
Deprecated
Deprecated as of 1.2 due to a typo in the method name.
Computes the longest common subsequence between the two |
| public CharSequence | Returns: the longest common subsequence found.first character sequence. left, final CharSequence second character sequence. right)Computes the longest common subsequence between the two |
| public int[][] | Returns: longest common substring length array.first character sequence. left, final CharSequence second character sequence. right)
Deprecated
Deprecated as of 1.10.
Computes the lcsLengthArray for the sake of doing the actual lcs calculation. |
| private static String |
| INSTANCE | back to summary |
|---|---|
| pack-priv static final LongestCommonSubsequence INSTANCE The singleton instance. | |
| LongestCommonSubsequence | back to summary |
|---|---|
| public LongestCommonSubsequence() Creates a new instance. | |
| algorithmB | back to summary |
|---|---|
| private static int[] algorithmB(final CharSequence left, final CharSequence right) An implementation of "ALG B" from Hirschberg's CACM '71 paper.
Assuming the first input sequence is of size
| |
| algorithmC | back to summary |
|---|---|
| private static String algorithmC(final CharSequence left, final CharSequence right) An implementation of "ALG C" from Hirschberg's CACM '71 paper.
Assuming the first input sequence is of size
| |
| apply | back to summary |
|---|---|
| public Integer apply(final CharSequence left, final CharSequence right) Implements org. Computes the longest common subsequence similarity score of two This method implements a more efficient version of LCS algorithm which has quadratic time and linear space complexity.
This method is based on newly implemented
| |
| logestCommonSubsequence | back to summary |
|---|---|
| public CharSequence logestCommonSubsequence(final CharSequence left, final CharSequence right)
Deprecated Deprecated as of 1.2 due to a typo in the method name.
Use Computes the longest common subsequence between the two
Note, a substring and subsequence are not necessarily the same thing. Indeed, For reference, we give the definition of a subsequence for the reader: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
| |
| longestCommonSubsequence | back to summary |
|---|---|
| public CharSequence longestCommonSubsequence(final CharSequence left, final CharSequence right) Computes the longest common subsequence between the two This method implements a more efficient version of LCS algorithm which although has quadratic time, it has linear space complexity.
Note, a substring and subsequence are not necessarily the same thing. Indeed, For reference, we give the definition of a subsequence for the reader: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
| |
| longestCommonSubstringLengthArray | back to summary |
|---|---|
| public int[][] longestCommonSubstringLengthArray(final CharSequence left, final CharSequence right)
Deprecated Deprecated as of 1.10. A more efficient implementation for calculating LCS is now available.
Use Computes the lcsLengthArray for the sake of doing the actual lcs calculation. This is the dynamic programming portion of the algorithm, and is the reason for the runtime complexity being O(m*n), where m=left.length() and n=right.length().
| |
| reverse | back to summary |
|---|---|
| private static String reverse(final CharSequence s) | |