The following code returns the length of longest common substring from given two strings in Java. For cases of around 10^6 characters, it is still not fast enough to get the answer in a few seconds. Any suggestions for efficiency will be greatly appreciated!
int longestCommonSubstring(String s, String t){ int maxlength = 0; for(int i=0; i<s.length(); i++){ for(int j=0; j<t.length(); j++){ int count = 0; int g = 0; while(i+g<s.length() && j+g<t.length()){ if(t.charAt(j+g)==s.charAt(i+g)){ count++; maxlength = Integer.max(count, maxlength); } else{ count = 0; } g++; } } } return maxlength;
}