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

Algorithm. We divide each of the matrices A and B into 9 submatrices with size n/3...
Algorithm. We divide each of the matrices A and B into 9 submatrices with size n/3 × n/3 each. The matrix C = A × B is obtained by using 25 multiplications and 30 additions of the submatrices. What is the recursive formula of the running time function T(n) of this algorithm? What is the running time of this algorithm (in Θ notation)?
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
Suppose you are choosing between the following two algorithms: Algorithm A: solves problems of size n...
Suppose you are choosing between the following two algorithms: Algorithm A: solves problems of size n by recursively solving two subproblems each of size (n-1) and then combining the solutions in constant time (take T(0) = 0). Algorithm B: solves problems of size n by recursively solving one subproblem of size (n-1) and then combining the solution in (log n) time (take T(1) = 0). Write down the recurrence relations for the above algorithms.                           Solve the recurrence relations...
data structures in the banker's algorithm is a vector of length m, where m is the...
data structures in the banker's algorithm is a vector of length m, where m is the number of resource types a variant of the resource allocation graph used if all resources have only a single instance system can allocate resources to each thread in some order and avoid a deadlock assigned to its own resource from thread Ti to resource Rj directed graph used to describe a deadlock deadlock avoidance algorithm used for resource allocation systems with multiple instances of...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT