Code from https://www.garysieling.com/blog/javascript-function-find-overlap-two-strings:
function findOverlap(a, b) {
if (b.length === 0) {
return '';
}
if (a.endsWith(b)) {
return b;
}
if (a.indexOf(b) >= 0) {
return b;
}
return findOverlap(a, b.substring(0, b.length - 1));
}
Some test cases:
findOverlap("12345", "aaa") // ""
findOverlap("12345", "12") // "12"
findOverlap("12345", "345") // "345"
findOverlap("12345", "3456") // "345"
findOverlap("12345", "111") // "1"
To solve your particular problem you could:
const haveOverlap = (string1, string2) => findOverlap(string1, string2).length >= 3;
console.log(haveOverlap('HelloWorld', 'Work')); // true