A palindrome is a string that reads the same backwards as forwards. For example, civic
, radar
, deed
, and racecar
are all palindromes. The definition I just gave is a non-recursive one: the string is equal to its reverse. Come up with a recursive definition of a palindrome, and write pseudocode for the following method. You can use indexing and range indexing to refer to characters and substrings of s
.
Here’s the pseudocode for a factorial function:
What is the asymptotic runtime class of this function in terms of n
?
Recall the count_e
method, which processed the string one character at a time (i.e., in an incremental fashion):
/** returns # of ‘e’ in string s. Pre: s is not null. */
public int count_e(String s):
if s.length() == 0:
return 0;
first = 0
if s[0] == ‘e’:
first = 1
return first + count_e(s[1..end])
Write a different version of this method that has the same spec, but solves the problem using the divide-and-conquer paradigm.