Question

In: Computer Science

Suppose we have a substring of length m and text of size n. Write an algorithm...

Suppose we have a substring of length m and text of size n. Write an algorithm to find out if the substring is present in the text or not. What is the complexity of your algorithm in terms of m and n.

Solutions

Expert Solution

Algorithm for the above question is provided below.

// Check substring is present in text
boolean isSubstringPresent(string substring, string text)
  
   // Find the length of substring
   Find the length of substring and store in variable m
  
   // Find the length of text
   Find the length of text and store in variable n
  
   // Loop through the text
   For i=0 to n-m
  
       // Loop through the substring
       For j=0 to m
      
           // If do not match break from the loop
           if text(i+j)!=substring(j)
               break;
      
       // if the substring matches
       if j=m
      
           // Return true
           return true
          
   //    Return false
   return false

Running time complexity:

The outer for loop parse fro 0 to n-m.

The inner for loop parse through 0 to m.

So the running time complexity = O((n-m)*m) =O(nm)


Related Solutions

Suppose we have a random sample of size 50 from a N(μ,σ2) PDF. We wish to...
Suppose we have a random sample of size 50 from a N(μ,σ2) PDF. We wish to test H0: μ=10 versus H1: μ=10. The sample moments are x ̄ = 13.4508 and s2 = 65.8016. (a) Find the critical region C and test the null hypothesis at the 5% level. What is your decision? (b) What is the p-value for your decision? (c) What is a 95% confidence interval for μ?
Suppose we find that, the average engine size in our sample of size n = 30...
Suppose we find that, the average engine size in our sample of size n = 30 is 192 cubic inches, with a standard deviation of 104 cubic inches. Use these statistics to compute a 90% confidence interval of population mean, that is, the average engine size for all.
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
We have a random sample of size n (which is large), and we wish to test...
We have a random sample of size n (which is large), and we wish to test - Ho: X~UNIF(0,1) vs Ha: X~exp(1). How would you conduct hypothesis testing? Describe procedure.
Write a program that takes a set of m numbers up to size n and save...
Write a program that takes a set of m numbers up to size n and save them in an array. The program then allows you to search for any number in the array with O(1).
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
Suppose that we performed the algorithm SELECT whose running time is O(n) on 133 elements, and...
Suppose that we performed the algorithm SELECT whose running time is O(n) on 133 elements, and found the median of medians x by making groups of 5 elements. What is the maximum number of elements which are guaranteed to be greater than equals to x (without counting x, itself)?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT