Question

ChangeString (s,x):    change = ""    for i from 1 to lenght(s):        ignore...

ChangeString (s,x):
   change = ""
   for i from 1 to lenght(s):
       ignore = false
       for k from 1 to length(x):
           if s[i] == x[k]:
               ignore = true
       if ignore == false:
           change += s[i] //assume this line is O(1) time
   return change


isTheSame (s, t, x):
   a = ChangeString (s, x)
   b = ChangeString (t, x)

   if length(a) != length(b):
       return false
   for i from 1 to lenghth(a):
       if a[i] != b[i]:
           return false
   return true

What is the worst case run time of isTheSame? Justify.

Homework Answers

Answer #1

As there are two independent nested "for"loop in ChangeString() so it will take n^2 time

isTheSame (s, t, x):
   a = ChangeString (s, x) //It take n^2 time
   b = ChangeString (t, x) // it also take n^2 time

   if length(a) != length(b): //It will take some constant time
       return false
   for i from 1 to lenghth(a): //it will take n time
       if a[i] != b[i]: //it will take constant time
           return false
   return true

Total time will be

(n^2) + (n^2) + c1 + n + c2

=2*(n^2) + n + C

In big O notation will take upper bound so we have

O() time complexity for  isTheSame​​​​​​​()

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
Restricted structures such as stack and queue are fast, but they do not support access in...
Restricted structures such as stack and queue are fast, but they do not support access in the key field mode. Group of answer choices True False Big O analysis evaluates an algorithm based on its _________ performance. Group of answer choices A. average-case B. best-case C. worst-case Which of the following algorithms is the fastest in speed? Group of answer choices A. Polynomial time algorithm B. Linear time algorithm C. Exponential time algorithm The following code gives an implementation of...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
public int linearSearch2(ArrayList<Integer> pList, Integer pKey) { for (int i = 0; i <= pList.size() /...
public int linearSearch2(ArrayList<Integer> pList, Integer pKey) { for (int i = 0; i <= pList.size() / 2; ++i) { if (pList.get(i).equals(pKey)) { return i; } else if (pList.get(pList.size() - 1 - i).equals(pKey)) { return pList.size() - 1 - i; } } return -1; } ----- Q1)Which function f(n) counts how many times the key operation(s) is(are) performed as a function of the list size n when pKey is not in pList (the worst case behavior of linearSearch2() occurs when pKey...
For the first order decomposition of H2O2(aq), given k = 3.60 x 10-3 s-1 and the...
For the first order decomposition of H2O2(aq), given k = 3.60 x 10-3 s-1 and the initial concentration of [H2O2]o is 0.882 M, determine : (a) the time at which [H2O2]t decreases to 0.600 M; (b) what will be the concentration of [H2O2]t after 225 s and (c) find the half-life of the reaction.
1. In the Solow model without exogenous technological change, per capita income will grow in the...
1. In the Solow model without exogenous technological change, per capita income will grow in the long term as long as the country has an initial level of capital below the steady state level of capital (k o < k ⋅) TRUE OR FALSE? 2. In the Solow model without exogenous technological change, per capita income will grow in the short term as long as the country has an initial level of capital below the steady state level of capital...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
How can I alter this Java method so that I convert and then return every sentence...
How can I alter this Java method so that I convert and then return every sentence in the array of strings instead of just the first sentence? public static char[] charConvert ( String [] sentences ) { int totLength = sentences[0].length() + sentences[1].length() + sentences[2].length() + sentences[3].length() + sentences[4].length(); char [] letter = new char[totLength]; int x = 0; int y = 0;    while ( y < sentences[x].length() ) { switch ( sentences[x].charAt(y) ) { case 'a': case 'A':...
- Please show all work and explain, I will thumbs up if done correctly -             ...
- Please show all work and explain, I will thumbs up if done correctly -              Boolean isSorted; for i = 1 to A:length -1             isSorted = true;             for j = A:length downto i + 1                   if A[j] < A[ j - 1] exchange A[j] with A[j -1] isSorted = false;                         if (isSorted) break; What is the best case running time of the above algorithm? What is the worst case running time of the above algorithm?...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT YOU CANT USE fopen, fclose , etc *****You are ONLY ALLOWED TO USE SYSTEM CALL I/O such as read, write, open and close (System I/O functions) You will write a C program and the way to use it on command line is as follows: ./myprogram file a t   newfile where both file and newfile are text files, “a” is a single character ( of...
True or false. If false, explain why. a. The relation G = H – T S...
True or false. If false, explain why. a. The relation G = H – T S is valid for all processes. b. G = A + pV. c. G is undefined for a process in which T changes. d. G = 0 for a reversible phase change at constant T and p. e. G = 0 for a chemical reaction in equilibroum at constant T and p. f. sysS  surrS is positive for every irreversible process. g. fH298 is...