7
$\begingroup$

The longest common substring (LCS) of two input strings $s,t$ is a common substring (in both of them) of maximum length. We can relax the constraints to generalize the problem: find a common substring of length $k$. We can then use binary search to find the maximum $k$. This takes time $\cal O(n \lg n)$ provided that solving the relaxed problem takes linear time.

Finding a $k$-common substring can be solved using a rolling hash:

  1. Compute hash values of all $k$-length substrings of $s$ and $t$.
  2. If a hash of $s$ coincides with a hash of $t,ドル then we've found a $k$-length common substring.

Step 1 uses a rolling hash to achieve linear time but I can't see how we can perform step 2. in linear time. Any suggestions?

asked May 26, 2014 at 22:22
$\endgroup$
2
  • 1
    $\begingroup$ Use a hash table. (Of sorts. You've already computed the hashes.) $\endgroup$ Commented May 26, 2014 at 23:06
  • $\begingroup$ I still can't see how. Could you please write an answer? $\endgroup$ Commented May 27, 2014 at 9:21

1 Answer 1

2
$\begingroup$

In order to implement step 2, use a hash table. Add all the hashes of the $k$-length substrings of $s$ to the table. For each $k$-length substring of $t,ドル look it up on the table. This takes expected linear time for a large enough hash table.

answered May 27, 2014 at 15:01
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.