如何高效获取两个字符串的相同子串
1个回答
展开全部
没有试过中文处理,有一个例子希望可以作为您的起点:
public static String longestCommonSubstring(String first, String second) {
String tmp = "";
String max = "";
for (int i=0; i < first.length(); i++){
for (int j = 0; j < second.length(); j++){
for (int k = 1; (k+i) <= first.length() && (k+j) <= second.length(); k++){
if (first.substring(i, k + i).equals(second.substring(j, k + j))){
tmp = first.substring(i, k + i);
}
else{
if (tmp.length() > max.length())
max = tmp;
tmp = "";
}
}
if (tmp.length() > max.length())
max = tmp;
tmp = "";
}
}
return max;
}
public static String longestCommonSubstring(String first, String second) {
String tmp = "";
String max = "";
for (int i=0; i < first.length(); i++){
for (int j = 0; j < second.length(); j++){
for (int k = 1; (k+i) <= first.length() && (k+j) <= second.length(); k++){
if (first.substring(i, k + i).equals(second.substring(j, k + j))){
tmp = first.substring(i, k + i);
}
else{
if (tmp.length() > max.length())
max = tmp;
tmp = "";
}
}
if (tmp.length() > max.length())
max = tmp;
tmp = "";
}
}
return max;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询