Skip to content

Commit 0a2ed9e

Browse files
PolurivalegonSchiele
authored andcommitted
Update LongestCommonSubsequence.java
for example from the book with words 'fish' and 'fosh' where is a mistake.
1 parent 468fda7 commit 0a2ed9e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

09_dynamic_programming/java/01_longest_common_subsequence/src/LongestCommonSubsequence.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class LongestCommonSubsequence {
44
public static void main(String[] args) {
5-
// if (word_a[i] == word_b[1]) {
5+
// if (word_a[i] == word_b[j]) {
66
// cell[i][j] = cell[i - 1][j - 1] + 1;
77
// } else {
88
// cell[i][j] = Math.Max(cell[i - 1][j], cell[i][j - 1]);
@@ -24,7 +24,11 @@ public static void main(String[] args) {
2424
}
2525
} else {
2626
// The letters don't match.
27-
if (i > 0 && j > 0) {
27+
if (i == 0 && j > 0) {
28+
cell[i][j] = cell[i][j - 1];
29+
} else if (i > 0 && j == 0) {
30+
cell[i][j] = cell[i - 1][j];
31+
} else if (i > 0 && j > 0) {
2832
cell[i][j] = Math.max(cell[i - 1][j], cell[i][j - 1]);
2933
} else {
3034
cell[i][j] = 0;
@@ -47,4 +51,4 @@ private static void printResult(int[][] arr) {
4751
}
4852
}
4953

50-
}
54+
}

0 commit comments

Comments
 (0)