Skip to content

Instantly share code, notes, and snippets.

@hadielmougy
Created October 24, 2016 23:30
Show Gist options
  • Save hadielmougy/3d91950acd6cea07b5cdf0da8975915e to your computer and use it in GitHub Desktop.
Save hadielmougy/3d91950acd6cea07b5cdf0da8975915e to your computer and use it in GitHub Desktop.
LongestCommonSubstring
package com.algorithms;
/**
* Created by Hadi on 10/25/2016.
*/
public class LongestCommonSubstring {
public static void main(String[] args){
System.out.println(longestCommonSubstring("blue","cluees"));
}
private static int longestCommonSubstring(String blue, String clues) {
int[][] V = new int[blue.length()+1][clues.length()+1];
for(int i=1;i<=blue.length();i++)
for(int j =1;j<=clues.length();j++){
if(blue.charAt(i-1)==clues.charAt(j-1)){
V[i][j] = V[i-1][j-1] + 1;
}else{
V[i][j] = Math.max(V[i-1][j],V[i][j-1]);
}
}
return V[blue.length()][clues.length()];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment