Question

Let T(n) be the runtime of the following isPalindrome() method which accepts a string of length...

Let T(n) be the runtime of the following isPalindrome() method which accepts a string of length n: public boolean isPalindrome(String str) { if (str.length() < 2) return true; if (str.charAt(0) != str.charAt(str.length()-1)) return false; return isPalindrome(str.substring(1, str.length()-1)); } Assuming each line of code (like each semicolon) counts as one instruction, choose the most appropriate recurrence that describes the runtime of isPalindrome(). Select one: a. T(n) = 3n + T(n-2), T(1) = 1, T(0) = 1

b. T(n) = 3n + T(n-1) , T(1) = 1, T(0) = 1

c. T(n) = 3 + T(n-2), T(1) = 1, T(0) = 1

d. T(n) = 3 + 2T(n-1), T(1) = 1, T(0) = 1

e. T(n) = 3 + T(n-1), T(0) = 1

Homework Answers

Answer #1

Ans: c). T(n) = 3 + T(n-2), T(1) = 1, T(0) = 1

=============================

First let's check our base case:

if (str.length() < 2) return true;

So for T(0) as well as T(1) we only execute one instruction. So they are equal to 1.

Now, for some n > 2 we check the first and last character and pass the remaining string of length n-2 as input for next function call. Also in this process we executed 3 statements.

So T(n) = 3 + T(n-2)

T(n-2) is term for the next call to isPalindrome with substring from 1 to n-1 passed. 3 is the term for the 3 statements executed.

hence option c is correct and remaining are incorrect.

========================

for any query comment

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
public class PalindromeChecker { /** * Method that checks if a phrase or word is *...
public class PalindromeChecker { /** * Method that checks if a phrase or word is * a Palindrome * * @param str * Represents a string input * * @return true * True if the string is a Palindrome, * false otherwise */ public static boolean isPalindrome(String str) { if (str == null) { return false; } str = str.toLowerCase(); String temp = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((ch...
public class Mystery { public static String mystery(String str, int input) { String result = "";...
public class Mystery { public static String mystery(String str, int input) { String result = ""; for (int i = 0; i < str.length() - 1; i++) { if (input == 0) { str = ""; result = str; } if (input == -2) { result = str.substring(2, 4); } if (input == 1) { result = str.substring(0, 1); } if (input == 2) { result = str.substring(0, 2); } if (input == 3) { result = str.substring(2, 3); }...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) {...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) { String str = input.toLowerCase(); return bar(str); } private String bar(String s) { if (s.length() <= 1)   {return "";} else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));} else if (s.length() % 2 == 1) {return "*" + bar(s+"n");} else {return "";} } Which line(s) indicate the name of the helper method? Which line(s) contains recursive call(s)? How many * are in...
C++ only: Please implement a function that accepts a string parameter as well as two character...
C++ only: Please implement a function that accepts a string parameter as well as two character arguments and a boolean parameter. Your function should return the number of times it finds the character arguments inside the string parameter. When both of the passed character arguments are found in the string parameter, set the boolean argument to true. Otherwise, set that boolean argument to false. Return -1 if the string argument is the empty string. The declaration for this function will...
Master Theorem: Let T(n) = aT(n/b) + f(n) for some constants a ≥ 1, b >...
Master Theorem: Let T(n) = aT(n/b) + f(n) for some constants a ≥ 1, b > 1. (1). If f(n) = O(n logb a− ) for some constant > 0, then T(n) = Θ(n logb a ). (2). If f(n) = Θ(n logb a ), then T(n) = Θ(n logb a log n). (3). If f(n) = Ω(n logb a+ ) for some constant > 0, and af(n/b) ≤ cf(n) for some constant c < 1, for all large n,...
Considering the following code segment, answer the multiple choice questions. Which line(s) indicate the base case...
Considering the following code segment, answer the multiple choice questions. Which line(s) indicate the base case of the recursion? Which line(s) of code make recursion move toward the base case? public String foo(String input) { String str = input.toLowerCase(); return bar(str); } private String bar(String s) { if (s.length() <= 1)   {return "";} else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));} else if (s.length() % 2 == 1) {return "*" + bar(s+"n");} else {return "";} }
Consider the following recursive algorithm. Algorithm Test (T[0..n − 1]) //Input: An array T[0..n − 1]...
Consider the following recursive algorithm. Algorithm Test (T[0..n − 1]) //Input: An array T[0..n − 1] of real numbers if n = 1 return T[0] else temp ← Test (T[0..n − 2]) if temp ≥ T[n − 1] return temp else return T[n − 1] a. What does this algorithm compute? b. Set up a recurrence relation for the algorithm’s basic operation count and solve it.
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
C# Please Write Unit tests for a boolean method validateStudentID() that accepts a string. It validates...
C# Please Write Unit tests for a boolean method validateStudentID() that accepts a string. It validates a DMACC 900 number. Then write the code to make the tests pass. Test 1. A valid 900 as input (method returns true) Test 2. An invalid 900 number -- does not start with 9  (method returns false) Test 3. An invalid 900 number -- second character is not zero (method returns false) Test 4. An invalid 900 number -- contains digits  (method returns false) Write...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a pointer to char str[] */ void array_to_ptr () { int n=0, len; char str[ ] = "Hello World!"; len = strlen(str); for( n=0; n<len; n++) {     putc(str[n], stdout); } printf("\nlength = %d\n", n); } int contains (char * str, char c); int * makearray(int n); int main (void) { printf("Question #2 - array_to_ptr:\n"); array_to_ptr();   printf("\n------------------------------------\n\n"); printf("Question #3 - contains:\n"); printf("Test #1: "); if...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT