Maximum Suffix–Prefix Overlap
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) → intExamples
Example 1
first = "ababc"second = "bca"return = 2The longest matching suffix and prefix are both bc, of length 2.
Example 2
first = "aaaa"second = "aa"return = 2The full second string can overlap, so the answer is 2.
Example 3
first = "cat"second = "dog"return = 0No nonempty suffix-prefix pair agrees.
Constraints
0 <= first.length, second.length <= 2 × 10^4.- Both strings contain only lowercase English letters.