Can Anyone help me. Please explain the question i cant get it and logic behind it.


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
gnanagowthaman sankarAsked:
Who is Participating?
You need to look for the given substring (('sub'), the second argument of the two), somewhere inside the first-given string, ('string'), and ensure that 'sub' both starts and  ends some substring in the first argument, 'string'.

So in the first example, cat begins and ends catcowcat, and the length of the string catcowcat is 9. However, if there were another 'string' such as catcowcatdogcatcowcowcowcat, then you would be returning an int of length 15, since catcowcowcowcat is longer than catcowcat. You need to code for that in the search algorithm.

The third example is the same as that, but the second example wants you to return 3, because cow both starts and ends cow in 'string'.

As for the recursive nature of the question, you can adapt the method that I showed in your previous question on recursion.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.