From d0b43db438ea4343ab958c6da6c578b7313ea6be Mon Sep 17 00:00:00 2001 From: icoder211 <58630967+icoder211@users.noreply.github.com> Date: Sat, 7 Nov 2020 18:58:01 +0530 Subject: [PATCH 1/8] Using manual loop to fill DistanceMatrix with 0s --- DataStructures/Graphs/FloydWarshall.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/DataStructures/Graphs/FloydWarshall.java b/DataStructures/Graphs/FloydWarshall.java index 017d0cd4a56d..8f55c3b03396 100644 --- a/DataStructures/Graphs/FloydWarshall.java +++ b/DataStructures/Graphs/FloydWarshall.java @@ -14,7 +14,11 @@ public FloydWarshall(int numberofvertices) { [numberofvertices + 1]; // stores the value of distance from all the possible path form the source // vertex to destination vertex - Arrays.fill(DistanceMatrix, 0); + for(int i = 0;i < numberofvertices + 1; i++) { + for(int j = 0;j < numberofvertices + 1; j++) { + DistanceMatrix[i][j] = 0; + } + } this.numberofvertices = numberofvertices; } From 38db10387777460f63e6e21a4fbd6f8caddc66f8 Mon Sep 17 00:00:00 2001 From: icoder211 <58630967+icoder211@users.noreply.github.com> Date: Thu, 12 Nov 2020 22:47:59 +0530 Subject: [PATCH 2/8] Removed for loop because initialization by new keyword automatically fills the array with 0s As pointed out by @prashantdoshi28 --- DataStructures/Graphs/FloydWarshall.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/DataStructures/Graphs/FloydWarshall.java b/DataStructures/Graphs/FloydWarshall.java index 8f55c3b03396..71f1aa30fea7 100644 --- a/DataStructures/Graphs/FloydWarshall.java +++ b/DataStructures/Graphs/FloydWarshall.java @@ -14,11 +14,6 @@ public FloydWarshall(int numberofvertices) { [numberofvertices + 1]; // stores the value of distance from all the possible path form the source // vertex to destination vertex - for(int i = 0;i < numberofvertices + 1; i++) { - for(int j = 0;j < numberofvertices + 1; j++) { - DistanceMatrix[i][j] = 0; - } - } this.numberofvertices = numberofvertices; } From 4b55b7a16b7cef97a85575aca8c3b9e8aa9475d0 Mon Sep 17 00:00:00 2001 From: icoder211 <58630967+icoder211@users.noreply.github.com> Date: Thu, 12 Nov 2020 23:30:07 +0530 Subject: [PATCH 3/8] Fixes #2009 --- DataStructures/Graphs/FloydWarshall.java | 1 + 1 file changed, 1 insertion(+) diff --git a/DataStructures/Graphs/FloydWarshall.java b/DataStructures/Graphs/FloydWarshall.java index 71f1aa30fea7..1411e1d91c09 100644 --- a/DataStructures/Graphs/FloydWarshall.java +++ b/DataStructures/Graphs/FloydWarshall.java @@ -14,6 +14,7 @@ public FloydWarshall(int numberofvertices) { [numberofvertices + 1]; // stores the value of distance from all the possible path form the source // vertex to destination vertex + // here the distanceMatrix is initialized by 0s b default upon initialization with new keyword this.numberofvertices = numberofvertices; } From d9e35702e233b74f2531ed7645ebef83bf0a9733 Mon Sep 17 00:00:00 2001 From: icoder211 <58630967+icoder211@users.noreply.github.com> Date: Fri, 13 Nov 2020 02:08:03 +0530 Subject: [PATCH 4/8] Added Longest common substring, solves #1781 --- .../LongestCommonSubstring.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 DynamicProgramming/LongestCommonSubstring.java diff --git a/DynamicProgramming/LongestCommonSubstring.java b/DynamicProgramming/LongestCommonSubstring.java new file mode 100644 index 000000000000..1efc2a494313 --- /dev/null +++ b/DynamicProgramming/LongestCommonSubstring.java @@ -0,0 +1,27 @@ +public class LongestCommonSubstring { + + public static String string1, string2; + public static int[][] dp; + public static void main(String[] args) { + int res = 0; + string1 = "abbaf"; + string2 = "abcdef"; + int n = string1.length(); + int m = string2.length(); + dp = new int[n+1][m+1]; + for(int i = 0;i <= n;i++) { + dp[0][i] = 0; + dp[i][0] = 0; + } + + for(int i = 1;i <= n;i++) { + for(int j = 1;j <= m;j++) { + if(string1.charAt(i-1) == string2.charAt(j-1)) { + dp[i][j] = 1 + dp[i-1][j-1]; + res = Math.max(res, dp[i][j]); + }else dp[i][j] = 0; + } + } + System.out.println(res); + } +} From 7d9493aea8b27a45824572beeec3fef1fe68ccf6 Mon Sep 17 00:00:00 2001 From: icoder211 <58630967+icoder211@users.noreply.github.com> Date: Fri, 13 Nov 2020 02:19:08 +0530 Subject: [PATCH 5/8] Delete LongestCommonSubstring.java --- .../LongestCommonSubstring.java | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 DynamicProgramming/LongestCommonSubstring.java diff --git a/DynamicProgramming/LongestCommonSubstring.java b/DynamicProgramming/LongestCommonSubstring.java deleted file mode 100644 index 1efc2a494313..000000000000 --- a/DynamicProgramming/LongestCommonSubstring.java +++ /dev/null @@ -1,27 +0,0 @@ -public class LongestCommonSubstring { - - public static String string1, string2; - public static int[][] dp; - public static void main(String[] args) { - int res = 0; - string1 = "abbaf"; - string2 = "abcdef"; - int n = string1.length(); - int m = string2.length(); - dp = new int[n+1][m+1]; - for(int i = 0;i <= n;i++) { - dp[0][i] = 0; - dp[i][0] = 0; - } - - for(int i = 1;i <= n;i++) { - for(int j = 1;j <= m;j++) { - if(string1.charAt(i-1) == string2.charAt(j-1)) { - dp[i][j] = 1 + dp[i-1][j-1]; - res = Math.max(res, dp[i][j]); - }else dp[i][j] = 0; - } - } - System.out.println(res); - } -} From 4c151c6777bd84330ab86d5e129b72513dd63f88 Mon Sep 17 00:00:00 2001 From: icoder211 <58630967+icoder211@users.noreply.github.com> Date: Fri, 13 Nov 2020 02:23:35 +0530 Subject: [PATCH 6/8] Added Longest Common substring of two strings Fixes #1718 --- .../LongestCommonSubstring.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 DynamicProgramming/LongestCommonSubstring.java diff --git a/DynamicProgramming/LongestCommonSubstring.java b/DynamicProgramming/LongestCommonSubstring.java new file mode 100644 index 000000000000..1efc2a494313 --- /dev/null +++ b/DynamicProgramming/LongestCommonSubstring.java @@ -0,0 +1,27 @@ +public class LongestCommonSubstring { + + public static String string1, string2; + public static int[][] dp; + public static void main(String[] args) { + int res = 0; + string1 = "abbaf"; + string2 = "abcdef"; + int n = string1.length(); + int m = string2.length(); + dp = new int[n+1][m+1]; + for(int i = 0;i <= n;i++) { + dp[0][i] = 0; + dp[i][0] = 0; + } + + for(int i = 1;i <= n;i++) { + for(int j = 1;j <= m;j++) { + if(string1.charAt(i-1) == string2.charAt(j-1)) { + dp[i][j] = 1 + dp[i-1][j-1]; + res = Math.max(res, dp[i][j]); + }else dp[i][j] = 0; + } + } + System.out.println(res); + } +} From efb418a592ba6f1f6d54cdbfb618deb5a17e2d96 Mon Sep 17 00:00:00 2001 From: icoder211 <58630967+icoder211@users.noreply.github.com> Date: Fri, 13 Nov 2020 02:25:02 +0530 Subject: [PATCH 7/8] Revert "Added Longest Common substring of two strings" --- .../LongestCommonSubstring.java | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 DynamicProgramming/LongestCommonSubstring.java diff --git a/DynamicProgramming/LongestCommonSubstring.java b/DynamicProgramming/LongestCommonSubstring.java deleted file mode 100644 index 1efc2a494313..000000000000 --- a/DynamicProgramming/LongestCommonSubstring.java +++ /dev/null @@ -1,27 +0,0 @@ -public class LongestCommonSubstring { - - public static String string1, string2; - public static int[][] dp; - public static void main(String[] args) { - int res = 0; - string1 = "abbaf"; - string2 = "abcdef"; - int n = string1.length(); - int m = string2.length(); - dp = new int[n+1][m+1]; - for(int i = 0;i <= n;i++) { - dp[0][i] = 0; - dp[i][0] = 0; - } - - for(int i = 1;i <= n;i++) { - for(int j = 1;j <= m;j++) { - if(string1.charAt(i-1) == string2.charAt(j-1)) { - dp[i][j] = 1 + dp[i-1][j-1]; - res = Math.max(res, dp[i][j]); - }else dp[i][j] = 0; - } - } - System.out.println(res); - } -} From 0343228b6b8a946f95488e3d59b3ffa3042d1b44 Mon Sep 17 00:00:00 2001 From: icoder211 <58630967+icoder211@users.noreply.github.com> Date: Fri, 13 Nov 2020 02:39:23 +0530 Subject: [PATCH 8/8] added longest common substring Fixes #1718 --- .../LongestCommonSubstring.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 DynamicProgramming/LongestCommonSubstring.java diff --git a/DynamicProgramming/LongestCommonSubstring.java b/DynamicProgramming/LongestCommonSubstring.java new file mode 100644 index 000000000000..1efc2a494313 --- /dev/null +++ b/DynamicProgramming/LongestCommonSubstring.java @@ -0,0 +1,27 @@ +public class LongestCommonSubstring { + + public static String string1, string2; + public static int[][] dp; + public static void main(String[] args) { + int res = 0; + string1 = "abbaf"; + string2 = "abcdef"; + int n = string1.length(); + int m = string2.length(); + dp = new int[n+1][m+1]; + for(int i = 0;i <= n;i++) { + dp[0][i] = 0; + dp[i][0] = 0; + } + + for(int i = 1;i <= n;i++) { + for(int j = 1;j <= m;j++) { + if(string1.charAt(i-1) == string2.charAt(j-1)) { + dp[i][j] = 1 + dp[i-1][j-1]; + res = Math.max(res, dp[i][j]); + }else dp[i][j] = 0; + } + } + System.out.println(res); + } +}