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 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); }...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Let an, for n≥1, be the number of strings of length n over {0,1,2,3}, allowing repetitions,...
Let an, for n≥1, be the number of strings of length n over {0,1,2,3}, allowing repetitions, suchthat no string contains a 3 to the right of a 0. Find a recurrence relation and initial condition(s) for an.
This is for C++ A string is a palindrome if reverse of the string is same...
This is for C++ A string is a palindrome if reverse of the string is same as the original string. For example, “abba” is palindrome, but “abbc” is not palindrome. Basically a string with a plane of symmetry in the middle of it is considered a palindrome. For example, the following strings are palindromic: "abbbbba", "abba", "abbcbba", "a", etc. For this problem, you have an input string consisting of both lowercase or uppercase characters. You are allowed to pick and...
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,...
1. Suppose that the integer variables n and m have been initialized. Check all the correct...
1. Suppose that the integer variables n and m have been initialized. Check all the correct statements about the following code fragment. String s2 = (n>=m) ? "one" : ( (m<=2*n) ? "two": "three" ); Question 5 options: If m is strictly greater than 2n then the value of s2 is "two" If n is equal to m then the value of s2 is "two" If m is strictly greater than 2n then the value of s2 is "three" If...
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.
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...