Skip to content

Commit df30da1

Browse files
committed
Enhance comments & improve readability in LongestCommonSubsequence.java
1 parent 842ff52 commit df30da1

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/LongestCommonSubsequence.java

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,118 @@
11
package com.thealgorithms.dynamicprogramming;
22

3+
/**
4+
* This class implements the Longest Common Subsequence (LCS) problem.
5+
* The LCS of two sequences is the longest sequence that appears in both
6+
* sequences
7+
* in the same order, but not necessarily consecutively.
8+
*
9+
* This implementation uses dynamic programming to find the LCS of two strings.
10+
*/
311
final class LongestCommonSubsequence {
12+
413
private LongestCommonSubsequence() {
514
}
615

16+
/**
17+
* Returns the Longest Common Subsequence (LCS) of two given strings.
18+
*
19+
* @param str1 The first string.
20+
* @param str2 The second string.
21+
* @return The LCS of the two strings, or null if one of the strings is null.
22+
*/
723
public static String getLCS(String str1, String str2) {
8-
// At least one string is null
24+
// If either string is null, return null as LCS can't be computed.
925
if (str1 == null || str2 == null) {
1026
return null;
1127
}
12-
13-
// At least one string is empty
28+
// If either string is empty, return an empty string as LCS.
1429
if (str1.length() == 0 || str2.length() == 0) {
1530
return "";
1631
}
1732

33+
// Convert the strings into arrays of characters
1834
String[] arr1 = str1.split("");
1935
String[] arr2 = str2.split("");
2036

21-
// lcsMatrix[i][j] = LCS of first i elements of arr1 and first j characters of arr2
37+
// lcsMatrix[i][j] = LCS(first i characters of str1, first j characters of str2)
2238
int[][] lcsMatrix = new int[arr1.length + 1][arr2.length + 1];
2339

40+
// Base Case: Fill the LCS matrix 0th row & 0th column with 0s
41+
// as LCS of any string with an empty string is 0.
2442
for (int i = 0; i < arr1.length + 1; i++) {
2543
lcsMatrix[i][0] = 0;
2644
}
2745
for (int j = 1; j < arr2.length + 1; j++) {
2846
lcsMatrix[0][j] = 0;
2947
}
48+
49+
// Build the LCS matrix by comparing characters of str1 & str2
3050
for (int i = 1; i < arr1.length + 1; i++) {
3151
for (int j = 1; j < arr2.length + 1; j++) {
52+
// If characters match, the LCS increases by 1
3253
if (arr1[i - 1].equals(arr2[j - 1])) {
3354
lcsMatrix[i][j] = lcsMatrix[i - 1][j - 1] + 1;
3455
} else {
56+
// Otherwise, take the maximum of the left or above values
3557
lcsMatrix[i][j] = Math.max(lcsMatrix[i - 1][j], lcsMatrix[i][j - 1]);
3658
}
3759
}
3860
}
61+
62+
// Call helper function to reconstruct the LCS from the matrix
3963
return lcsString(str1, str2, lcsMatrix);
4064
}
4165

66+
/**
67+
* Reconstructs the LCS string from the LCS matrix.
68+
*
69+
* @param str1 The first string.
70+
* @param str2 The second string.
71+
* @param lcsMatrix The matrix storing the lengths of LCSs of substrings of str1
72+
* and str2.
73+
* @return The LCS string.
74+
*/
4275
public static String lcsString(String str1, String str2, int[][] lcsMatrix) {
43-
StringBuilder lcs = new StringBuilder();
44-
int i = str1.length();
45-
int j = str2.length();
76+
StringBuilder lcs = new StringBuilder(); // Hold the LCS characters.
77+
int i = str1.length(); // Start from the end of str1.
78+
int j = str2.length(); // Start from the end of str2.
79+
80+
// Trace back through the LCS matrix to reconstruct the LCS
4681
while (i > 0 && j > 0) {
82+
// If characters match, add to the LCS and move diagonally in the matrix
4783
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
4884
lcs.append(str1.charAt(i - 1));
4985
i--;
5086
j--;
5187
} else if (lcsMatrix[i - 1][j] > lcsMatrix[i][j - 1]) {
88+
// If the value above is larger, move up
5289
i--;
5390
} else {
91+
// If the value to the left is larger, move left
5492
j--;
5593
}
5694
}
57-
return lcs.reverse().toString();
95+
96+
return lcs.reverse().toString(); // LCS built in reverse, so reverse it back
5897
}
5998

99+
/**
100+
* Main method to run a sample test for the LCS implementation.
101+
*
102+
* @param args Command-line arguments (not used in this case).
103+
*/
60104
public static void main(String[] args) {
61105
String str1 = "DSGSHSRGSRHTRD";
62106
String str2 = "DATRGAGTSHS";
107+
108+
// Get the LCS of str1 and str2
63109
String lcs = getLCS(str1, str2);
64110

65-
// Print LCS
111+
// Print the results
66112
if (lcs != null) {
67113
System.out.println("String 1: " + str1);
68114
System.out.println("String 2: " + str2);
69-
System.out.println("LCS: " + lcs);
115+
System.out.println("Longest Common Subsequence (LCS): " + lcs);
70116
System.out.println("LCS length: " + lcs.length());
71117
}
72118
}

0 commit comments

Comments
 (0)