Skip to the content

The twin codices

Problem

Two copies of the same codex were saved from the fire at the abbey. They were made by different scribes, and each one skipped or changed letters in his own way. Brother Bernard, the librarian, wants to measure the longest passage that both copies keep identical: that one, at least, he can trust.

Write a function that takes a and b, two strings of lowercase letters with no spaces (they may be empty), and returns, as a whole number, the length of the longest run of consecutive letters that appears exactly as is in both. It can be anywhere in each string, but skipping letters is not allowed: it has to be consecutive in a and consecutive in b.

With "parchment" and "merchant": "rch" starts at the third letter of a and the third of b, it is 3 long, and no shared run is 4, so it returns 3. With "abxabcd" and "abcd", "ab" matches from the start, but "abcd" is longer: it returns 4.

If they share no letter at all, or either one is empty, return 0.

Examples

  • The example

    "parchment", "merchant" → 3

  • The first shared run is not the longest

    "abxabcd", "abcd" → 4

  • No letter in common

    "abc", "xyz" → 0

  • Both copies are identical

    "psalm", "psalm" → 5

  • Skipping letters is not allowed

    "abcde", "azbzczdze" → 1

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def shared_passage(a, b):
    pass

JavaScript

function sharedPassage(a, b) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →