diff --git a/DataStructures/Graphs/FloydWarshall.java b/DataStructures/Graphs/FloydWarshall.java index 017d0cd4a56d..1411e1d91c09 100644 --- a/DataStructures/Graphs/FloydWarshall.java +++ b/DataStructures/Graphs/FloydWarshall.java @@ -14,7 +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 - Arrays.fill(DistanceMatrix, 0); + // here the distanceMatrix is initialized by 0s b default upon initialization with new keyword this.numberofvertices = numberofvertices; } 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); + } +}