This page is still under construction.

Parts of this page are still being built. What you see may change.

Barn Echoes

Interview

Time limit1sMemory limit128 MB

Summary
Given two lowercase strings, find the length of the longest string that is a prefix of one and a suffix of the other.
Level

Easy2 of 10

Topics
String, Brute force, Implementation, String matching
Solved
No attempts yet

Problem

The cows love mooing in the barn because their moos echo back, though sometimes not perfectly. Bessie, an excellent secretary, has recorded the exact text of each moo both as it goes out and as it returns, and she wonders how much the two strings overlap.

You are given two strings. Each consists only of the lowercase letters a..z, and the total length of the two strings is between 1 and 80, inclusive. We call a string an overlap of the two strings if it is a prefix of one string and, at the same time, a suffix of the other string. Determine the length of the longest possible overlap between the two strings.

For example, consider these two moos:

moyooyoxyzooo
yzoooqyasdfljkamo

The end of the first string, 'yzooo', overlaps the start of the second string. The end of the second string, 'mo', overlaps the start of the first string. The longest overlap is 'yzooo', whose length is 5.

Input

  • Lines 1 and 2: Each line contains one string — a moo or its echo.

Output

  • Line 1: A single integer, the length of the longest overlap between the front of one string and the end of the other.

Hint

In the sample input above, 'abcxxxxabcx' is a prefix of the first string and a suffix of the second string, and its length is 11.

Examples2

  1. Example 1

    Input
    abcxxxxabcxabcd
    abcdxabcxxxxabcx
    
    Expected output
    11
    
  2. Example 2

    Input
    moyooyoxyzooo
    yzoooqyasdfljkamo
    
    Expected output
    5