From 0a2ed9e4bb8cff3d51abe83772f072d27bab1ad6 Mon Sep 17 00:00:00 2001 From: Yury Polshchikov Date: Sat, 27 May 2017 13:07:03 +0300 Subject: [PATCH] Update LongestCommonSubsequence.java for example from the book with words 'fish' and 'fosh' where is a mistake. --- .../src/LongestCommonSubsequence.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/09_dynamic_programming/java/01_longest_common_subsequence/src/LongestCommonSubsequence.java b/09_dynamic_programming/java/01_longest_common_subsequence/src/LongestCommonSubsequence.java index 20079f7..4e4b61c 100644 --- a/09_dynamic_programming/java/01_longest_common_subsequence/src/LongestCommonSubsequence.java +++ b/09_dynamic_programming/java/01_longest_common_subsequence/src/LongestCommonSubsequence.java @@ -2,7 +2,7 @@ import java.util.Arrays; public class LongestCommonSubsequence { public static void main(String[] args) { -// if (word_a[i] == word_b[1]) { +// if (word_a[i] == word_b[j]) { // cell[i][j] = cell[i - 1][j - 1] + 1; // } else { // cell[i][j] = Math.Max(cell[i - 1][j], cell[i][j - 1]); @@ -24,7 +24,11 @@ public class LongestCommonSubsequence { } } else { // The letters don't match. - if (i > 0 && j > 0) { + if (i == 0 && j > 0) { + cell[i][j] = cell[i][j - 1]; + } else if (i > 0 && j == 0) { + cell[i][j] = cell[i - 1][j]; + } else if (i > 0 && j > 0) { cell[i][j] = Math.max(cell[i - 1][j], cell[i][j - 1]); } else { cell[i][j] = 0; @@ -47,4 +51,4 @@ public class LongestCommonSubsequence { } } -} \ No newline at end of file +}