FastPrepMaximum Suffix–Prefix Overlap

Maximum Suffix–Prefix Overlap

Cresta logoCresta● EasyFULLTIMEOA
Learn

Problem statement

Given strings first and second, return the largest integer k such that the last k characters of first equal the first k characters of second.

The overlap may use an entire input string. Length 0 is always allowed. For this exercise, assume the inputs contain only lowercase English letters; an empty input therefore yields 0.

Function

maximumOverlap(first: String, second: String) → int

Examples

Example 1

first = "ababc"second = "bca"return = 2

The longest matching suffix and prefix are both bc, of length 2.

Example 2

first = "aaaa"second = "aa"return = 2

The full second string can overlap, so the answer is 2.

Example 3

first = "cat"second = "dog"return = 0

No nonempty suffix-prefix pair agrees.

Constraints

  • 0 <= first.length, second.length <= 2 × 10^4.
  • Both strings contain only lowercase English letters.

More Cresta problems

See Cresta hiring insights
public int maximumOverlap(String first, String second) {
    // write your code here
}
first"ababc"
second"bca"
expected2
Checking account…