previous | start | next

## Recursive Helper Functions

- Sometimes it is easier to find a recursive solution if you change the original problem slightly, then solve the problem using a recursive helper function.
- In our palindrome example, it is inefficient to construct new string objects in every step.
- Rather than creating a new string, this function simply skips over matching letter pairs by adjusting the `start` and `end` parameters with each recursive call.

  >     bool substring_is_palindrome(string s, int start, int end)
  >     {
  >        // separate case for substrings of length 0 and 1
  >        if (start >= end) return true;

  >        if (s[start] == s[end])
  >           // test substring that doesn't contain the first and last letters
  >           return substring_is_palindrome(s, start + 1, end - 1);
  >        else
  >           return false;
  >     }

- The new function is a helper function that the user of the `is_palindrome` function doesn't know about.

  >     bool is_palindrome(string s)
  >     {
  >        return substring_is_palidrome(s, 0, s.length() - 1);
  >     }

previous | start | next