public class MaxCommonSubString { public static void main(String[] args) { String a = "abcdefg"; String b = "cbcdge"; String max = ""; for(int i = 0; i < b.length(); i++) for(int j = i; j < b.length(); j++){ String sub = b.substring(i, j); if((a.indexOf(sub) != -1)&&(sub.length() > max.length())) max = sub; } System.out.println(max); } }