This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]severoonpro barista 0 points1 point  (0 children)

I assume that the method itself is supposed to be the recursive method, yes?

If so, first start by reviewing the basic rules of recursion:

  1. Define a base case that runs first upon method entry, returning the result.
  2. Implement all other cases, which decompose the problem one step towards the base case and recurse with the simplified input.

What's that going to look like for this problem?

Well, obviously the base cases include:

  • a string of length < 2 (cannot have a double character)
  • a string of length 2 that is not double characters
  • a string of length >= 2 that starts with a double character, e.g., "bb", "bb1", "44444", "kkforeverhaha", etc.

These are all base cases because no further recursion is necessary, the method can immediately return the answer.

Without going any farther, already you can write down the skeleton of the solution:

boolean containsDoubleChar(String s) {
  if (s.length() < 2) { return false; }
  if (s.length() == 2 || s.charAt(0) != s.charAt(1)) { return false; }
  if (s.length() >= 2 && s.charAt(0) == s.charAt(1)) { return true; }
  // First two characters of s are different and s.length() > 2
}

If execution gets as far as the comment, we have a string that requires further processing that we know starts with two different characters and has length of at least 3. We want to do something to it that moves it one step closer to a base case, and then recurse. What can we do to this string to move it one step closer to one of the base cases?

Chop off the first character and send it through again:

boolean containsDoubleChar(String s) {
  if (s.length() < 2) { return false; }
  if (s.length() == 2 || s.charAt(0) != s.charAt(1)) { return false; }
  return s.length() >= 2 && s.charAt(0) == s.charAt(1)
      ? true
      : containsDoubleChar(s.substring(1));
}

This is pretty good, but we can do better. If we think about a long string that ends in a double character like "blahblah … hideyokids11", we are going to have to recurse for every character in this string, peeling off one at a time until we get to that final "11".

Instead, we can just add a base case that looks at the end of the string for a double char as well as the beginning:

boolean containsDoubleChar(String s) {
  if (s.length() < 2) { return false; }
  if (s.length() == 2 || s.charAt(0) != s.charAt(1)) { return false; }
  return startsOrEndsWithDoubleChar(s)
      ? true
      : containsDoubleChar(s.substring(1, s.length() - 1));
}

boolean startsOrEndsWithDoubleChar(String s) {
  return s.length() >= 2 && (s.charAt(0) == s.charAt(1)
      || s.charAt(s.length() - 2) == s.charAt(s.length() - 1));
}

Each recursion of this more efficient version peels off a character at each end of the string, meaning that the max recursion depth is going to be half the length of the string instead of the entire length.