我们可以使用下面的代码来识别n个字符串中最长的公共子字符串
public static String identifyCommonSubStrOfNStr(String [] strArr){
String commonStr="";
String smallStr ="";
//identify smallest String
for (String s :strArr) {
if(smallStr.length()< s.length()){
smallStr=s;
}
}
String tempCom="";
char [] smallStrChars=smallStr.toCharArray();
for (char c: smallStrChars){
tempCom+= c;
for (String s :strArr){
if(!s.contains(tempCom)){
tempCom=c;
for (String s :strAarr){
if(!s.contains(tempCom)){
tempCom="";
break;
}
}
break;
}
}
if(tempCom!="" && tempCom.length()>commonStr.length()){
commonStr=tempCom;
}
}
return commonStr;
}