public class Test
{
public String search(String s1,String s2)
{
String max = "" ;
for ( int i = 0 ; i < s1.length(); i ++ )
{
for ( int j = i + 1 ; j <= s1.length(); j ++ )
{
String sub = s1.substring(i,j);
if ((s2.indexOf(sub) != - 1 ) && sub.length() > max.length())
{
max = sub;
}
}
}
return max;
}
public static void main(String[] args)
{
String s1 = " abedafghigj " ;
String s2 = " xyzabfddfigj " ;
String output = new Test().search(s1,s2);
System.out.println(output);
}
}
|