Data Structures & AlgorithmsHardGATE CS · ICPC regional

What is the length of the longest common SUBSTRING (contiguous) of "ABABCABBAC" and "BABCBACAB"?

  1. A. 7
  2. B. 6
  3. C. 3
  4. D. 4

Correct answer: D. 4

Let d[i][j] be the length of the common suffix ending at s[i] and t[j]. It resets to 0 on mismatch and extends by 1 on match, and the answer is the maximum d. Using the subsequence recurrence without the reset gives the wrong (larger) number.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic