Given a string and a non-empty substring sub, compute recursively the largest substring which starts and ends with sub and return its length.
strDist("catcowcat", "cat") → 9
strDist("catcowcat", "cow") → 3
strDist("cccatcowcatxx", "cat") → 9
public int strDist(String str, String sub) {
int strLen = str.length();
int subLen = sub.length();
if(strLen<subLen)
{
return 0;
}
if(str.substring(0,subLen).equals(sub))
{
if(str.substring(strLen-subLen,strLen).equals(sub))
{
return strLen;
}
else
{
return strDist(str.substring(0,strLen-1),sub);
}
}
else
{
return strDist(str.substring(1,strLen),sub);
}
}
how To Compile This without Import and user input
ReplyDelete