Last active
February 5, 2016 20:19
-
-
Save dgodfrey206/3a74becfb17a13a37f45 to your computer and use it in GitHub Desktop.
Java recursion #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a string and a non-empty substring sub, compute recursively the largest substring which starts and ends with sub and return its length. */ | |
public int strDist(String str, String sub) { | |
if (str.length() < sub.length()) return 0; | |
if (str.substring(0, sub.length()).equals(sub) && | |
str.substring(str.length() - sub.length(), str.length()).equals(sub)) | |
return str.length(); | |
int p1 = strDist(str.substring(1), sub); | |
int p2 = strDist(str.substring(0, str.length() - 1), sub); | |
return Math.max(p1, p2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://codingbat.com/prob/p195413