直接暴力解决了,思路比较好理解
毕竟用的是官方API
String a1="asnasdklasdhhelloa";
String a2="cvhelloaygghhelloa";
int c=0;int max=Integer.MIN_VALUE;String temp=null;
String lengthest=null;
for(int i=0;i<a2.length()-1;i++)
{
for(int j=i;j<a2.length();j++)
{
temp=a2.substring(i,j+1);
// if(a1.indexOf(temp)>=0)
if (a1.contains(temp))
{
if(temp.length()>max) {
max = temp.length();
lengthest=temp;
}
}
}
}
System.out.println("最长子串是"+lengthest);
System.out.println("最长子串长度是"+max);