您的位置:首页 > 其它

获取最大相同子串

2013-01-02 08:29 162 查看
class StringTest3
{
public static void main(String[] args)
{
String s1 = "Hello" ;
String s2 = "ell";
System.out.println(getMaxSubString(s2,s1));
}
/*获取最大相同子串*/
public static String getMaxSubString(String s1,String s2)
{
if (s1.length()<s2.length())//以短的作为子串
{
String temp = s2;
s2 = s1;
s1 = temp;
}
/*短的子串长度依次递减1,然后遍历*/
for(int i=0;i<s2.length();i++)
for (int j=0,k=s2.length()-i; k!=s2.length()+1;j++,k++ )
{
String temp = s2.substring(j,k);	//取从j字符开始的k个字符的子串
if (s1.indexOf(temp)!=-1)	//找不到子串则还回-1
{
return temp;
}
}
return "";//即没有相同子串
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: