Suppose we want to find the first occurrence of a string(Pattern) in a long sting (Text). Let thePattern has k characters P1P2 · · · Pk and Text has N characters A1A2 · · · AN. We can solve thisproblem using hash function. We first compute the hash function H(Pattern) and get a hash valueHp. We then compare this hash value Hp with the hash values formed from strings A1A2 · · · Ak,A2A3 · · · Ak+1, A3A4 · · · Ak+2, and so on until AN-k+1AN-k+2 · · · AN using the same hash function H.If for some substring of Text H(AiAi+1 · · · Ai+k-1) is same as Hp, then we compare the strings(Pattern and AiAi+1 · · · Ai+k-1) character by character to verify the match. We return the position i(in A) if the strings actually do match, and we continue if the match is false. To make this ideaefficient, we need to efficiently compute the hash value of string Ai+1Ai+2 · · · Ai+k using the hashvalue of AiAi+1 · · · Ai+k-1 for 1 <= i<=N-k.Give the hash function H for which we can compute H(Ai+1Ai+2 · · · Ai+k ) using the answer of H(AiAi+1 · · ·Ai+k-1) in O(1). Also show how this computation can be done in O(1). Give the complete pseudo code ofthe pattern matching algorithm described above.
#Sales Offer!| Get upto 25% Off: